tags:

views:

166

answers:

2

I coded a mpi matrix multification program, which use scanf("%d", &size), designate matrix size, then I defined int matrix[size*size], but when I complied it, it reported that matrix is undeclared. Please tell me why, or what my problem is!

According Ed's suggestion, I changed the matrix definition to if(myid == 0) block, but got the same err! Now I post my code, please help me find out where I made mistakes! thank you!

int size;

int main(int argc, char* argv[]) {

int myid, numprocs; int *p; MPI_Status status; int i,j,k; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD,&myid); MPI_Comm_size(MPI_COMM_WORLD, &numprocs); if(myid == 0) {
scanf("%d", &size); int matrix1[size*size]; int matrix2[size*size]; int matrix3[size*size]; int section = size/numprocs; int tail = size % numprocs; srand((unsigned)time(NULL)); for( i=0; i printf("Matrix1 is: \n"); for( i=0; i for( j=0; j printf("\n");
}
printf("\n");
printf("Matrix2 is: \n");

A: 

Reformatted code would be nice...

One problem is that you haven't declared the size variable. Another problem is that the [size] notation for declaring arrays is only good for sizes that are known at compile time. You want to use malloc() instead.

mch
thanks, I think scanf("%d", int matrix[size];successfully on my own computer. but on mpi environment, it sucks! that is where I comfused!
Johnson
A: 

You don't actually need to define a MAX_SIZE if you use dynamic memory allocation.

#include <stdio.h>
#include <stdlib.h>
...
   scanf("%d", &size); 
   int *matrix1 = (int *) malloc(size*size*sizeof(int)); 
   int *matrix2 = (int *) malloc(size*size*sizeof(int)); 
   int *matrix3 = (int *) malloc(size*size*sizeof(int));
...
Edu