fork download
  1. #include<mpi.h>
  2. #include<omp.h>
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #define TOTAL_AT_RANK 10
  6.  
  7. int main(int argc, char **argv) {
  8. int i, my_max, total_max;
  9. int rank, np;
  10. MPI_Init(&argc, &argv);
  11. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  12. MPI_Comm_size(MPI_COMM_WORLD, &np);
  13.  
  14. printf("procs = %d and rank = %d\n",np, rank);
  15.  
  16. int* array = (int*) malloc(sizeof(int) * np * TOTAL_AT_RANK);
  17.  
  18. int arr_sz = np * TOTAL_AT_RANK;
  19. int start_index = TOTAL_AT_RANK * rank;
  20. int end_index = start_index + TOTAL_AT_RANK;
  21. if(rank == 0) {
  22. for(i=0; i<arr_sz; i++) {
  23. array[i] = i;
  24. }
  25. }
  26. MPI_Bcast(array, arr_sz, MPI_INT, 0, MPI_COMM_WORLD);
  27. my_max= 0;
  28. #pragma omp parallel for default(shared) private(i) reduction(max:my_max)
  29. for(i=start_index; i<end_index; i++) {
  30. if(my_max < array[i]){
  31. my_max = array[i];
  32. }
  33. }
  34. printf("process %d my_max = %d\n",rank, my_max);
  35. MPI_Reduce(&my_max, &total_max, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
  36.  
  37. //Verify the result at root
  38. if(rank == 0) {
  39. int expected = 0;
  40. for(i=0; i<np * TOTAL_AT_RANK; i++) {
  41. if(expected < array[i]){
  42. expected = array[i];
  43. }
  44. }
  45. if(total_max == expected) printf("0: Test SUCCESS\n");
  46. else printf("0: Test FAILED\n");
  47. printf("expected = %d and total_max = %d\n",expected, total_max );
  48. }
  49.  
  50. free(array);
  51. MPI_Finalize();
  52. return 0;
  53. }
Success #stdin #stdout #stderr 0.27s 40896KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted