Программирование с использованием MPI

"mpi-test.c"
#include <stdio.h>
#include <openmpi/mpi.h>
#include <stdlib.h>
 
int main(int argc, char** argv) {
    // Initialize the MPI environment
    MPI_Init(NULL, NULL);
 
    // Get the number of processes
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);
 
    // Get the rank of the process
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
 
    // Get the name of the processor
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    int name_len;
    MPI_Get_processor_name(processor_name, &name_len);
 
    // Print off a hello world message
    printf("Hello world from processor %s, rank %d"
           " out of %d processors\n",
           processor_name, world_rank, world_size);
 
   unsigned long int i;
   double sum;
   int sign;
 
   srand(world_rank+1);
 
   unsigned long int N = 10000000000;
 
   unsigned long int N_CURRENT_PROC = N / world_size;
 
   for (i=world_rank*N_CURRENT_PROC; i < (world_rank+1)*N_CURRENT_PROC; i++)
   {
      sign = rand();
      if (sign > RAND_MAX/2)
          sign = 1;
      else
          sign = -1;
 
      sum += sign*rand();
   }
 
    printf("THREAD %d: %f\n",world_rank,sum);
 
    // Finalize the MPI environment.
    MPI_Finalize();
}
"p1-mpi.c"
#include <stdio.h>
#include <openmpi/mpi.h>
#include <stdlib.h>
#include <math.h>
 
int main(int argc, char** argv) {
    // Initialize the MPI environment
    MPI_Init(NULL, NULL);
 
    // Get the number of processes
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);
 
    // Get the rank of the process
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
 
    // Get the name of the processor
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    int name_len;
    MPI_Get_processor_name(processor_name, &name_len);
 
    // Print off a hello world message
    printf("Hello world from processor %s, rank %d"
           " out of %d processors\n",
           processor_name, world_rank, world_size);
 
 
   srand(1);
   struct sphere {
     double x;
     double y;
     double z;
   };
 
   struct sphere spheres[1000];
 
   unsigned long int i,j;
   double x,y,r;
 
   for (i=0; i<1000; i++)
   {
        spheres[i].x = (double)rand()/RAND_MAX;
        spheres[i].y = (double)rand()/RAND_MAX;
        spheres[i].z = (double)rand()/RAND_MAX;
   }
 
// Sort spheres along Z coordinate  
 
 
   double sum;
   int sign;
 
   srand(world_rank+1);
 
   unsigned long int N = 1000;
 
   unsigned long int N_CURRENT_PROC = N / world_size;
 
   double dobeg[N_CURRENT_PROC];
 
   for (i=world_rank*N_CURRENT_PROC; i < (world_rank+1)*N_CURRENT_PROC; i++)
   {
       for (j=0;j<1000;j++)
       {
           x = (double)rand()/RAND_MAX;
           y = (double)rand()/RAND_MAX;
           r = sqrt(pow((x-spheres[j].x),2) + pow((y-spheres[j].y),2));
           //printf("%f\n",r);
           if (r < 0.1)
           {
               dobeg[i-world_rank*N_CURRENT_PROC] = spheres[j].z;
               break;
           }
           else dobeg[i-world_rank*N_CURRENT_PROC] = 1;
 
           //printf("%d\n",dobeg[i-world_rank*N_CURRENT_PROC]);
       }
   }
 
    unsigned long int spectrum[100];
    for (i=0;i<100;i++) spectrum[i] = 0;
 
    for (i=0;i<N_CURRENT_PROC;i++)
    {
        spectrum[(long int)floor(dobeg[i]*100)] ++;
        //printf("%d\n",dobeg[i]);
    }
 
    char outfilename[50] = "";
    sprintf(outfilename,"dobeg-%d.dat",world_rank);
    //printf(outfilename);
    FILE *outfile = fopen(outfilename,"w");
 
    for (i=0;i<100;i++)
    {
        fprintf(outfile,"%d   %d\n",i,spectrum[i]);
    }
 
    fclose(outfile);
 
    printf("THREAD %d: %f\n",world_rank,sum);
 
    // Finalize the MPI environment.
    MPI_Finalize();
}