fork download
  1. #include <iostream>
  2. #include <mpi.h>
  3. #include <pthread.h>
  4.  
  5. void* myApp(void* x) {
  6. int* k = ((int*)x);
  7. switch (k[0]) {
  8. case 0:
  9. pthread_exit((void*)(k[1] + k[2]));
  10. break;
  11. case 1:
  12. pthread_exit((void*)(k[1] * k[2]));
  13. break;
  14. case 2:
  15. pthread_exit((void*)(k[1]-k[2]));
  16. break;
  17. }
  18. return NULL;
  19. }
  20.  
  21. int main(int argc, char* argv[]) {
  22. #define N 9
  23.  
  24. int array[N];
  25. int i;
  26. int id;
  27. int ierr;
  28. int master = 0;
  29. int p;
  30. static int r8_pi = 3;
  31. MPI_Status status;
  32. int Rt, R;
  33. int S = 0;
  34.  
  35. ierr = MPI_Init(&argc, &argv);
  36. MPI_Comm_size(MPI_COMM_WORLD, &p);
  37. MPI_Comm_rank(MPI_COMM_WORLD, &id);
  38.  
  39. if (id == 0) {
  40. for (i = 0; i < N; i++) {
  41. array[i] = i * r8_pi;
  42. }
  43. }
  44.  
  45. MPI_Bcast(array, N, MPI_INT, master, MPI_COMM_WORLD);
  46. int x[3];
  47. pthread_t th;
  48.  
  49. for (int i = id; i < N - 2; i += 3) {
  50. x[0] = i % 3;
  51. if (i % 3 != 0)
  52. x[i % 3] = array[i];
  53. if ((i + 1) % 3 != 0)
  54. x[(i + 1) % 3] = array[i + 1];
  55. if ((i + 2) % 3 != 0)
  56. x[(i + 2) % 3] = array[i + 2];
  57.  
  58. pthread_create(&th, NULL, myApp, (void *)x);
  59. pthread_join(th, (void **)&Rt);
  60. S += Rt;
  61. }
  62.  
  63. if (id != 0) {
  64. printf("P= %d, S= %d\n", id, S);
  65. MPI_Send(&S, 1, MPI_INT, master, 1, MPI_COMM_WORLD);
  66. } else {
  67. R = S;
  68. printf("P= %d, S= %d\n", id, S);
  69. for (i = 1; i < p; i++) {
  70. MPI_Recv(&S, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, &status);
  71. R += S;
  72. }
  73. }
  74.  
  75. system("pause");
  76. MPI_Finalize();
  77. return 0;
  78.  
  79. #undef N
  80. }
  81.  
  82.  
Success #stdin #stdout #stderr 0.28s 40524KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected '{' in "void* myApp(void* x) {"
Execution halted