Thread: MPI read matrix malloc problem

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    79

    MPI read matrix malloc problem

    Hello I would welcome any help to eliminate the error in the code below. It is for open a file and input a 2d matrix and distribute blocks to the processes. I take some warnings and error in malloc . Please see also my_malloc function. Thanks

    [code]
    #include <stdio.h>
    #include <mpi.h>
    #include "MyliB.h"

    typedef double dtype;
    #define mpitype MPI_DOUBLE

    int main(int argc, char *argv){

    dtype **a;
    dtype *b;
    dtype *c_block;
    dtype *c;
    dtype **storage;
    int i,j;
    int id;
    int m;
    int n;
    int nprime;
    int p;
    int rows;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &id);
    MPI_Comm_size(MPI_COMM_WORLD, &p);

    read_row_striped_matrix(argv[1], (void *) &a, (void *) &storage, mpitype, &m, &n, MPI_COMM_WORLD);
    rows = BLOCK_SIZE(id,p,m);
    print_row_striped_matrix((void **) a, mpitype, m, n, MPI_COMM_WORLD);
    MPI_Finalize();
    return 0;
    }


    void read_row_striped_matrix(
    char *s, /*IN-File name ****/
    void ***subs, /*OUT -2D submatrix indices **/
    void **storage, /*OUT -Submatrix stored here */
    MPI_Datatype dtype, /*IN-Matrix element type */
    int *m, /*OUT-Matrix rows**************/
    int *n, /*OUT-Matrix cols**************/
    MPI_Comm comm) /*IN-Communicator**************/

    {
    int datum_size; /*Size of matrix element*/
    int i;
    int id;
    FILE *infileptr;
    int local_rows;
    void **lptr;
    int p;
    void *rptr;
    MPI_Status status;
    int x;

    MPI_Comm_size (comm, &p);
    MPI_Comm_rank (comm, &id);
    datum_size = get_size (dtype); /*See other function**/

    /*Process p-1 opens file reads size of matrix and broadcasts matrix dimensions to other procs*/


    if (id == (p-1))
    { infileptr = fopen (s, "r");
    if (infileptr == NULL) *m=0;
    else {
    fread (m,sizeof(int),1,infileptr);
    fread (n,sizeof(int),1,infileptr);
    }
    }
    MPI_Bcast (m,1,MPI_INT,p-1,comm);

    if (!(*m)) MPI_Abort (MPI_COMM_WORLD, OPEN_FILE_ERROR);

    MPI_Bcast (n,1,MPI_INT,p-1,comm);

    local_rows=BLOCK_SIZE(id,p,*m);

    /*Dynamically allocate matrix. Allow double subscripting through 'a'*/

    *storage = (void *) my_malloc(id, local_rows * *n * datum_size);
    *subs = (void **) my_malloc (id, local_rows * PTR_SIZE);*/
    lptr = (void *) &(*subs[0]);
    rptr = (void *) *storage;
    for (i=0;i<local_rows;i++)
    { *(lptr++)= (void *) rptr;
    rptr += *n *datum_size;
    }

    /*Process p-1 reads blocks of rows from a file and sends each block to the correct destination process. The last block it keeeps */

    if (id == (p-1))
    { for (i=0; i< p-1; i++)
    {x= fread(*storage, datum_size, BLOCK_SIZE(i,p,*m)* *n, infileptr);
    MPI_Send (*storage, BLOCK_SIZE(i,p,*m) * *n,dtype,i, DATA_MSG,comm);
    }
    x=fread (*storage, datum_size,local_rows * *n, infileptr);
    fclose(infileptr);
    }
    else
    MPI_Recv (*storage, local_rows * *n, dtype, p-1, DATA_MSG, comm, &status);
    }

    int get_size (MPI_Datatype t)
    { if (t == MPI_BYTE) return sizeof(char);
    if (t == MPI_DOUBLE) return sizeof(double);
    if (t == MPI_FLOAT) return sizeof(float);
    if (t == MPI_INT) return sizeof(int);
    printf("ERROR: UNRECOGNIZED ARGUMENT TO FUNTION 'get_size'\n");
    fflush (stdout);
    MPI_Abort (MPI_COMM_WORLD, TYPE_ERROR);
    }

    void *my_malloc( int id, int bytes)
    {
    void *buffer;
    if((buffer = malloc ((size_t) bytes)) == NULL)
    { printf ("ERROR: MALLOC FAILED FOR PROCESS %d\n", id);
    fflush (stdout);
    MPI_Abort(MPI_COMM_WORLD,MALLOC_ERROR);
    }
    return buffer;
    }
    void print_row_striped_matrix(
    void **a,
    MPI_Datatype dtype,
    int m,
    int n,
    MPI_Comm comm)
    {
    MPI_Status status;
    void *bstorage;
    void **b;

    int datum_size;
    int i;
    int id;
    int local_rows;
    int max_block_size;
    int prompt;
    int p;

    MPI_Comm_rank(comm,&id);
    MPI_Comm_size(comm,&p);
    local_rows = BLOCK_SIZE(id,p,m);
    if (!id)
    { print_submatrix (a, dtype,local_rows,n);
    if (p>1)
    { datum_size = get_size(dtype);
    max_block_size = BLOCK_SIZE(p-1,p,m);
    bstorage = my_malloc(id, max_block_size * n * datum_size);
    b = (void **) my_malloc (id, max_block_size * datum_size);
    b[0] = bstorage;
    for(i=1;max_block_size;i++)
    {
    b[i] = b[i-1] + n*datum_size;
    }


    for (i=1;i <p;i++)
    { MPI_Send (&prompt, 1, MPI_INT, i, PROMPT_MSG, MPI_COMM_WORLD);
    MPI_Recv(bstorage, BLOCK_SIZE(i,p,m)*n, dtype, i, RESPONSE_MSG,MPI_COMM_WORLD, &status);
    print_submatrix(b, dtype, BLOCK_SIZE(i,p,m),n);
    }
    free(*b);
    free(bstorage);
    }
    putchar ('\n');
    }
    else {
    MPI_Recv (&prompt, 1, MPI_INT, 0, PROMPT_MSG, MPI_COMM_WORLD, &status);
    MPI_Send (*a, local_rows *n, dtype, 0, RESPONSE_MSG, MPI_COMM_WORLD);
    }
    }

    void print_submatrix(
    void **a,
    MPI_Datatype dtype,
    int rows,
    int cols)
    {
    int i,j;
    for(i=0;i <rows;i++)
    {
    for (j=0; j < cols; j++)
    {
    if (dtype == MPI_DOUBLE)
    printf("%6.3f", ((double **)a )[i][j]);
    else {
    if (dtype == MPI_FLOAT)
    printf("%6.3f", ((float **)a )[i][j]);

    else if (dtype == MPI_INT)
    printf ("%6d", ((int **)a )[i][j]);
    }
    }
    putchar('\n');
    }
    }

    Code:
    mpicc -o test matr_vect_mult.c 
    matr_vect_mult.c: In function ‘main’:
    matr_vect_mult.c:23: warning: passing argument 2 of ‘MPI_Init’ from incompatible pointer type
    matr_vect_mult.c:27: warning: passing argument 1 of ‘read_row_striped_matrix’ makes pointer from integer without a cast
    matr_vect_mult.c: In function ‘read_row_striped_matrix’:
    matr_vect_mult.c:81: warning: cast to pointer from integer of different size
    matr_vect_mult.c:82: warning: cast to pointer from integer of different size
    matr_vect_mult.c: At top level:
    matr_vect_mult.c:114: error: conflicting types for ‘my_malloc’
    matr_vect_mult.c:81: error: previous implicit declaration of ‘my_malloc’ was here
    matr_vect_mult.c: In function ‘my_malloc’:
    matr_vect_mult.c:117: warning: incompatible implicit declaration of built-in function ‘malloc’
    matr_vect_mult.c: In function ‘print_row_striped_matrix’:
    matr_vect_mult.c:165: warning: incompatible implicit declaration of built-in function ‘free’
    matr_vect_mult.c: At top level:
    matr_vect_mult.c:176: warning: conflicting types for ‘print_submatrix’
    matr_vect_mult.c:147: warning: previous implicit declaration of ‘print_submatrix’ was here
    I know its annoying but try just to find about the error

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Eh, put code tags around your code or Salem will smite thee!
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program 2D matrix multiplication using malloc
    By college_kid in forum C Programming
    Replies: 5
    Last Post: 04-03-2009, 10:04 AM
  2. Malloc and MPI
    By moddinati in forum C Programming
    Replies: 17
    Last Post: 03-07-2008, 07:55 PM
  3. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 04:28 PM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM