int server()
{
unsigned long long start = , end = ; // 1..59049
MPI_Status status;
int i=0;
ofstream output;
char file_name[FILENAME_MAX] = {};
sprintf(file_name,"./d.txt");
for(d=start; d < end; d++) {
init_output(file_name,&output);
output << d << endl;
output.flush();
close_output(&output);
MPI_Recv(NULL, 0, MPI_UNSIGNED_LONG_LONG, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
MPI_Send(&d,1,MPI_UNSIGNED_LONG_LONG,status.MPI_SOURCE,0,MPI_COMM_WORLD);
}
d=0;
for(i=0;i<4095;i++) {
MPI_Recv(NULL, 0, MPI_UNSIGNED_LONG_LONG, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
MPI_Send(&d,1,MPI_UNSIGNED_LONG_LONG,status.MPI_SOURCE,0,MPI_COMM_WORLD);
}
return 0;
}
#include <cstdio>
#include <vector>
#include <mpi.h>
#include "tools.h"
using namespace std;
unsigned long long d = 1;
unsigned long long get_new_d(int ccpu)
{
MPI_Status status;
//printf("%d: Start sender\n",ccpu);
if ( MPI_SUCCESS != MPI_Sendrecv(NULL,0,MPI_UNSIGNED_LONG_LONG,0,0,&d,1,MPI_UNSIGNED_LONG_LONG,0,MPI_ANY_TAG,MPI_COMM_WORLD,&status))
return 0;
return d;
}
int server()
{
MPI_Status status;
for(; d < 10; d++) {
MPI_Recv(NULL, 0, MPI_UNSIGNED_LONG_LONG, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
MPI_Send(&d,1,MPI_UNSIGNED_LONG_LONG,status.MPI_SOURCE,0,MPI_COMM_WORLD);
}
return 0;
}
int main(int argc, char *argv[])
{
int ccpu, nprocs;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &ccpu);
if(ccpu == 0)
{
server();
printf("end server\n");
}
else
{
for(;;)
{
d=get_new_d(ccpu);
if(d == 0)
break;
printf("%d: %lld\n",ccpu,d);
}
}
MPI_Finalize();
return 0;
}