I'm trying to take a randomly generated array on root 0, vary it slightly and randomly, and send each variation to another processor. Here is my code so far:
#include "stdio.h"
#include "stdlib.h"
#include "mpi.h"
#include "math.h"
int main(int argc, char **argv) {
int N = 32;
int dim = 3;
float a = 10.0;
int size, rank, i, j, k, q;
float **C;
float rijx, rijy, rijz, rij, Vij, E=0;
float stepsize = 0.05;
double Start_time, End_time, Elapse_time;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
C = (float **)malloc(N * sizeof(float*)); // 32 particles
for (i = 0; i < N; i++) {
C[i]=(float *)malloc(dim*sizeof(float)); // x, y, z
}
MPI_Barrier(MPI_COMM_WORLD);
if(rank == 0) {
Start_time = MPI_Wtime();
}
if (rank == 0) {
for(i = 0; i < N; i++) {
for(j = 0; j < dim; j++) {
C[i][j] = (float)rand()/(float)(RAND_MAX/a);
//printf("%f\n",C[i][j]);
}
}
}
// this is to generate some slight variations in the array
float** randomsteps(float **matrix) {
for(i = 0; i < N; i = i+(rand()%(32/size))) {
for (j = 0; j < dim; j++) {
if(i%2 == 0) {
C[i][j] = C[i][j]+stepsize;
if(C[i][j] > 10) {
C[i][j] = C[i][j] - 10;
}
} else {
C[i][j] = C[i][j]-stepsize;
if(C[i][j] < 0) {
C[i][j] = C[i][j] + 10;
}
}
}
}
return C;
}
// and here I try to send the array
if(rank == 0) {
for(i=0; i<size; i++) {
C = randomsteps(C);
MPI_Send(&C, N*3, MPI_FLOAT, i, 10+i, MPI_COMM_WORLD);
}
}
if(rank != 0) {
for(i=0; i<size; i++) {
MPI_Recv(&C, N*3, MPI_FLOAT, 0, 10+i, MPI_COMM_WORLD, &status);
}
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}
An obvious problem with the code is that the way the random numbers are generated is somewhat naive (it gives the same values every time I run the program). That's something I can work on later.
For right now, I'm just wondering - what is wrong with the way I'm sending and receiving the array? I'm having a lot of trouble wrapping my head around how it is best to format data when sending and receiving using MPI. How would I go about fixing this part of the code?
Thanks in advance for the help!