ctest.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include <stdio.h>
  2. #include "mpi.h"
  3. main(int argc, char *argv[])
  4. {
  5. MPI_Request sreq[10], sreq2[10], rreq[10], rreq2[10];
  6. int sbuf[10],sbuf2[10],rbuf[10],rbuf2[10];
  7. int tag;
  8. MPI_Status status[10], sr_status;
  9. int i,j;
  10. MPI_Comm comm2;
  11. int flag;
  12. MPI_Group mygroup;
  13. char pname[MPI_MAX_PROCESSOR_NAME];
  14. int pnamelen;
  15. int position, temp;
  16. printf("Time: %f\n",MPI_Wtime());
  17. MPI_Initialized(&flag);
  18. printf("MPI is initialized = %d\n",flag);
  19. MPI_Init(NULL,NULL);
  20. MPI_Get_processor_name(pname,&pnamelen);
  21. printf("Processor name: %s (len=%d)\n",pname,pnamelen);
  22. #if 0
  23. MPI_Comm_dup(MPI_COMM_WORLD,&comm2);
  24. #endif
  25. #if 0
  26. MPI_Comm_split(MPI_COMM_WORLD,42,99,&comm2);
  27. #endif
  28. #if 1
  29. MPI_Comm_group(MPI_COMM_WORLD,&mygroup);
  30. MPI_Comm_create(MPI_COMM_WORLD,mygroup,&comm2);
  31. #endif
  32. MPI_Initialized(&flag);
  33. printf("MPI is initialized = %d\n",flag);
  34. for (i=0; i<5; i++)
  35. {
  36. tag=100+i;
  37. printf("COMWORLD Post ireceive tag %d\n",tag);
  38. MPI_Irecv(&rbuf[2*i],1,MPI_2INT,
  39. 0,tag,MPI_COMM_WORLD,&rreq[i]);
  40. }
  41. for (i=0; i<5; i++)
  42. {
  43. sbuf2[i]=1000+10*i;
  44. tag=100+i;
  45. printf("COM2 Post isend %d tag %d\n",sbuf2[i],tag);
  46. MPI_Isend(&sbuf2[i],1,MPI_INT,0,tag,comm2,&sreq2[i]);
  47. }
  48. for (i=0; i<5; i++)
  49. {
  50. sbuf[2*i]=10*i;
  51. sbuf[2*i+1]=10*i+1;
  52. tag=100+(4-i);
  53. printf("COMWORLD Post irsend %d tag %d\n",sbuf[i],tag);
  54. MPI_Irsend(&sbuf[2*i],1,MPI_2INT,0,tag,MPI_COMM_WORLD,&sreq[i]);
  55. }
  56. printf("Time: %f\n",MPI_Wtime());
  57. MPI_Waitall(5,sreq,status);
  58. MPI_Waitall(5,rreq,status);
  59. printf("Waiting for COMWORLD send/receives\n");
  60. for (i=0; i<5; i++)
  61. printf("tag %d rbuf= %d %d\n",status[i].MPI_TAG,rbuf[2*i],rbuf[2*i+1]);
  62. for (i=0; i<5; i++)
  63. {
  64. tag=100+i;
  65. printf("COM2 Post receive tag %d\n",tag);
  66. MPI_Irecv(&rbuf2[i],1,MPI_INT,
  67. 0,tag,comm2,&rreq2[i]);
  68. }
  69. MPI_Waitall(5,sreq2,status);
  70. MPI_Waitall(5,rreq2,status);
  71. printf("Waiting for COM2 send/receive\n");
  72. for (i=0; i<5; i++)
  73. printf("tag %d rbuf= %d\n",status[i].MPI_TAG,rbuf2[i]);
  74. /*
  75. * pack/unpack
  76. */
  77. position=0;
  78. for (i=0; i<5; i++)
  79. {
  80. temp=100+i;
  81. MPI_Pack(&temp, 1, MPI_INT, sbuf, 20, &position, MPI_COMM_WORLD);
  82. }
  83. MPI_Isend( sbuf, position, MPI_PACKED, 0, 0, MPI_COMM_WORLD,&sreq[0]);
  84. MPI_Irecv( rbuf, position, MPI_PACKED, 0, 0, MPI_COMM_WORLD, &rreq[0] );
  85. MPI_Waitall(1,rreq,status);
  86. printf("Pack/send/unpack: \n");
  87. position=0;
  88. for (i=0; i<5; i++)
  89. {
  90. MPI_Unpack(rbuf,20,&position,&temp,1,MPI_INT,MPI_COMM_WORLD);
  91. printf("%d\n",temp);
  92. }
  93. /*
  94. * sendrecv
  95. */
  96. sbuf[0]=42;
  97. rbuf[0]=0;
  98. sr_status.MPI_SOURCE= -1;
  99. sr_status.MPI_TAG= -1;
  100. MPI_Sendrecv(sbuf,1,MPI_INT,0,127,
  101. rbuf,1,MPI_INT,MPI_ANY_SOURCE,MPI_ANY_TAG,
  102. MPI_COMM_WORLD,&sr_status);
  103. printf("Done with MPI_Sendrecv, rbuf=%d source=%d tag=%d\n",
  104. rbuf[0],sr_status.MPI_SOURCE,sr_status.MPI_TAG);
  105. /*
  106. * Send to nowhere
  107. */
  108. printf("Send to MPI_PROC_NULL\n");
  109. MPI_Send(sbuf, 1, MPI_INT, MPI_PROC_NULL, 77, MPI_COMM_WORLD);
  110. printf("Receive from MPI_PROC_NULL\n");
  111. MPI_Recv(rbuf, 1, MPI_INT, MPI_PROC_NULL, 78, MPI_COMM_WORLD, status);
  112. printf(" status: source=%d tag=%d\n",
  113. status[0].MPI_SOURCE,status[0].MPI_TAG);
  114. /*
  115. * Finish up
  116. */
  117. MPI_Finalize();
  118. for (i=0; i<5; i++)
  119. {
  120. printf("Time: %f\n",MPI_Wtime());
  121. sleep(1);
  122. }
  123. }