CollectiveCommunicationsSelf Test with solution
Collective Communications Self Test with solution
Self Test1.We want to do a simple broadcast of variableabc[7] in processor 0 to the same location in allother processors of the communicator. What isthe correct syntax of the call to do thisbroadcast?a) MPI_Bcast (&abc[7], 1, MPI_REAL, 0, comm)b) MPl Bcast (&abc, 7, MPl REAL, 0, comm )c) MPl_Broadcast (&abc[7], 1, MPI_REAL, 0, comm )
Self Test 1. We want to do a simple broadcast of variable abc[7] in processor 0 to the same location in all other processors of the communicator. What is the correct syntax of the call to do this broadcast? a) MPI_Bcast ( &abc[7], 1, MPI_REAL, 0, comm ) b) MPI_Bcast ( &abc, 7, MPI_REAL, 0, comm ) c) MPI_Broadcast ( &abc[7], 1, MPI_REAL, 0, comm )
Answera) Correct!b) Sorry, that's not correct.the first argument should be the location abc[7l. Thesecond argument should be the number of elementsto transfer. For this question, we transfer 1 datavalue.c) Sorry, that's not correct.the correct function name is MPl BcastO. Otherwisethe arguments are correct
Answer a) Correct! b) Sorry, that's not correct. • the first argument should be the location abc[7]. The second argument should be the number of elements to transfer. For this question, we transfer 1 data value. c) Sorry, that's not correct. • the correct function name is MPI_Bcast(). Otherwise, the arguments are correct
Self Test2.Each processor has a local array a with 50elements. Each local array is a slice of a largerglobal array. We wish to compute the average(mean) of all elements in the global array. Ourpreferred approach is to add all of the dataelement-by-element onto the root processor,sum elements of the resulting array, divide, andbroadcast the result to all processes. Whichsequence of calls will accomplish this? Assumevariables are typed and initialized appropriately
Self Test 2. Each processor has a local array a with 50 elements. Each local array is a slice of a larger global array. We wish to compute the average (mean) of all elements in the global array. Our preferred approach is to add all of the data element-by-element onto the root processor, sum elements of the resulting array, divide, and broadcast the result to all processes. Which sequence of calls will accomplish this? Assume variables are typed and initialized appropriately
Self Testa)start=0;final=49;count = final - start + 1;mysum= O;for(i=start;i<=final;++i)mysum +=ailMPlReduce(&mysum,&sum,1,MPl_REALMPI SUM, root, comm );total count=nprocs*count;if(myrank==root)average=sum/total countMPl_Bcast(&average,1,MPl_REAL,root, comm);
Self Test a) start = 0; final = 49; count = final - start + 1; mysum = 0; for ( i=start; i<=final; ++i ) mysum +=a[i]; MPI_Reduce ( &mysum, &sum, 1, MPI_REAL, MPI_SUM, root, comm ); total_count=nprocs * count; if ( my_rank==root ) average=sum / total_count; MPI_Bcast ( &average, 1, MPI_REAL, root, comm );