Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char *message;
- message= (char*)malloc(msg_size);
- int main(int argc, char **argv)
- {
- double startwtime, endwtime;
- float elapsed_time, bandwidth;
- int my_id, next_id; /* process id-s */
- int p; /* number of processes */
- char* message; /* storage for the message */
- int i, k, max_msgs, msg_size, v;
- MPI_Status status; /* return status for receive */
- MPI_Init( &argc, &argv );
- MPI_Comm_rank( MPI_COMM_WORLD, &my_id );
- MPI_Comm_size( MPI_COMM_WORLD, &p );
- if (argc < 3)
- {
- fprintf (stderr, "need msg count and msg size as paramsn");
- goto EXIT;
- }
- if ((sscanf (argv[1], "%d", &max_msgs) < 1) ||
- (sscanf (argv[2], "%d", &msg_size) < 1))
- {
- fprintf (stderr, "need msg count and msg size as paramsn");
- goto EXIT;
- }
- **message = (char*)malloc (msg_size);**
- if (argc > 3) v=1; else v=0; /*are we in verbose mode*/
- /* don't start timer until everybody is ok */
- MPI_Barrier(MPI_COMM_WORLD);
- int t=0;
- if( my_id == 0 ) {
- startwtime = MPI_Wtime();
- // do max_msgs times:
- // send message of size msg_size chars to process 1
- // receive message of size msg_size chars from process p-1
- while(t<max_msgs) {
- MPI_Send((char *) message, msg_size, MPI_CHAR, 1 , 0, MPI_COMM_WORLD);
- MPI_Recv((char *) message, msg_size, MPI_CHAR, p-1, 0, MPI_COMM_WORLD, &status);
- t++;
- }
- MPI_Barrier(MPI_COMM_WORLD);
- endwtime = MPI_Wtime();
- elapsed_time = endwtime-startwtime;
- bandwidth = 2.0 * max_msgs * msg_size / (elapsed_time);
- printf("Number, size of messages: %3d , %3d n", max_msgs, msg_size);
- fflush(stdout);
- printf("Wallclock time = %f secondsn", elapsed_time );
- fflush(stdout);
- printf("Bandwidth = %f bytes per secondn", bandwidth);
- fflush(stdout);
- } else if( my_id == p-1 ) {
- // do max_msgs times:
- // receive message of size msg_size from process to the left
- // send message of size msg_size to process to the right (p-1 sends to 0)
- while(t<max_msgs) {
- MPI_Send((char *) message, msg_size, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
- MPI_Recv((char *) message, msg_size, MPI_CHAR, my_id-1, 0, MPI_COMM_WORLD, &status);
- t++;
- }
- } else {
- while(t<max_msgs) {
- MPI_Send((char *) message, msg_size, MPI_CHAR, my_id+1, 0, MPI_COMM_WORLD);
- MPI_Recv((char *) message, msg_size, MPI_CHAR, my_id-1, 0, MPI_COMM_WORLD, &status);
- t++;
- }
- }
- MPI_Barrier(MPI_COMM_WORLD);
- EXIT:
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement