fork download
  1.  
  2. # include <iostream>
  3.  
  4. # include <mpi.h>
  5.  
  6. #include<pthread.h>
  7.  
  8. void* myApp(void *x){
  9.  
  10. int *k = ((int*)x);
  11.  
  12. switch(k[0]){
  13.  
  14. case 0:
  15.  
  16. pthread_exit((void *)(k[1]+k[2]));
  17.  
  18. break;
  19.  
  20. case 1:
  21.  
  22. pthread_exit((void *)(k[1]*k[2]));
  23.  
  24. break;
  25.  
  26. case 2:
  27.  
  28. pthread_exit((void *)(k[1]-k[2]));
  29.  
  30. break;
  31.  
  32. }
  33.  
  34. return NULL;
  35.  
  36. }
  37.  
  38. int main ( int argc, char *argv[] ) {
  39.  
  40. # define N 9
  41.  
  42. int array[N];
  43.  
  44. int i;
  45.  
  46. int id;
  47.  
  48. int ierr;
  49.  
  50. int master = 0;
  51.  
  52. int p;
  53.  
  54. static int r8_pi = 3;
  55.  
  56. MPI_Status status;
  57.  
  58. int Rt, R;
  59.  
  60. int S = 0;
  61.  
  62. ierr = MPI_Init ( &argc, &argv );
  63.  
  64. MPI_Comm_size ( MPI_COMM_WORLD, &p );
  65.  
  66. MPI_Comm_rank ( MPI_COMM_WORLD, &id );
  67.  
  68. if ( id == 0 ) {
  69.  
  70. for ( i = 0; i < N; i++ ) {
  71.  
  72. array[i] = i * r8_pi;
  73.  
  74. }
  75.  
  76. }
  77.  
  78. MPI_Bcast ( array, N, MPI_INT, master, MPI_COMM_WORLD );
  79.  
  80. int x[3];
  81.  
  82. pthread_t th;
  83.  
  84. for(int i= id; i<N-2; i+=3){
  85.  
  86. x[0]= i%3;
  87.  
  88. if(i%3!=0)
  89.  
  90. x[i%3] =array[i];
  91.  
  92. if((i+1)%3!=0)
  93.  
  94. x[(i+1)%3] = array[i + 1];
  95.  
  96. if((i+2)%3!=0)
  97.  
  98. x[(i+2)%3 ] = array[i + 2];
  99.  
  100. pthread_create(&th, NULL, myApp, (void*)x);
  101.  
  102. pthread_join(th, (void**)&Rt);
  103.  
  104. S += Rt;
  105.  
  106. }
  107.  
  108. if ( id != 0 ) {
  109.  
  110. printf("P= %d, S= %d\n", id, S);
  111.  
  112. MPI_Send ( &S, 1, MPI_INT, master, 1, MPI_COMM_WORLD );
  113.  
  114. }
  115.  
  116. else {
  117.  
  118. R = S;
  119.  
  120. printf("P= %d, S= %d\n", id, S);
  121.  
  122. for ( i = 1; i < p; i++ )
  123.  
  124. {
  125.  
  126. MPI_Recv ( &S, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, &status );
  127.  
  128. R += S;
  129.  
  130. }
  131.  
  132. }
  133.  
  134. system("pause");
  135.  
  136. MPI_Finalize ( );
  137.  
  138. return 0;
  139.  
  140. # undef N
  141.  
  142. }
  143.  
  144.  
Success #stdin #stdout #stderr 0.25s 40416KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected '{' in "void* myApp(void *x){"
Execution halted