MPI malloc

Συζητήσεις για την γλώσσα C και C++

Συντονιστές: WebDev Moderators, Super-Moderators

Απάντηση
JinTonic
Δημοσιεύσεις: 9
Εγγραφή: 11 Ιουν 2010 15:18

MPI malloc

Δημοσίευση από JinTonic » 27 Ιουν 2010 21:38

το παρακατω προγραμμα δουλευει για 2διαστασεων πινακες με λιγες γραμμεσ και στηλες, προσπαθω να το κανω να δουλευει για nxn πινακες. χρεισιμοποιω malloc αλλα καπου εχει σφαλμα και στα μισα της εκτελεσης κανει διακοπη και εμφανιζει το παρακατω μηνυμα.


sending 6 rows to task 1
sending 5 rows to task 2
sending 5 rows to task 3
Master =0, mtype=1
Master =0, mtype=1
Master =0, mtype=1
offset =6
row =5
[electra:05490] *** Process received signal ***
[electra:05490] Signal: Segmentation fault (11)
[electra:05490] Signal code: Address not mapped (1)
[electra:05490] Failing at address: 0x600000006
--------------------------------------------------------------------------
mpiexec noticed that process rank 2 with PID 5490 on node electra.it.uom.gr exited on signal 11 (Seg mentation fault).
--------------------------------------------------------------------------
αν μπορει καποιος να με κατευθηνει που εχω λαθος και πως θα το λυσω.

Κώδικας: Επιλογή όλων


#include <stdio.h>
#include "mpi.h"
#include <stdlib.h> 
#define MASTER 0		/* taskid of first task */
#define FROM_MASTER 1		/* setting a message type */
#define FROM_WORKER 2		/* setting a message type */
#define MS 16

int ms=MS;

MPI_Status status;
main&#40;int argc, char **argv&#41; 
&#123;
int numtasks,			/* number of tasks in partition */
    taskid,			/* a task identifier */
    numworkers,			/* number of worker tasks */
    source,			/* task id of message source */
    dest,			/* task id of message destination */
    nbytes,			/* number of bytes in message */
    mtype,			/* message type */
    rows,                      	/* rows of matrix A sent to each worker */
    averow, extra, offset,      /* used to determine rows sent to each worker */
    i, j, k,			/* misc */
    count;
int **a, **b, **c;

MPI_Init&#40;&argc, &argv&#41;;
MPI_Comm_rank&#40;MPI_COMM_WORLD, &taskid&#41;;
MPI_Comm_size&#40;MPI_COMM_WORLD, &numtasks&#41;;
numworkers = numtasks-1;

/**************************** master task ************************************/
if &#40;taskid == MASTER&#41; &#123;
  printf&#40;"Number of worker tasks = %d\n",numworkers&#41;; 

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~memory allocation master~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  a = malloc&#40;ms * sizeof&#40;*a&#41;&#41;;
  b = malloc&#40;ms * sizeof&#40;*b&#41;&#41;;
  c = malloc&#40;ms * sizeof&#40;*c&#41;&#41;;
  if&#40;a != NULL||b!= NULL ||c != NULL&#41;
  &#123;
    for&#40;i = 0; i < ms; i++&#41; /* Loop through each row pointer to allocate memory for columns*/
    &#123;
         /* Set p&#91;i&#93; pointer to a block of memory for 'column' number of integers */
         a&#91;i&#93; = malloc&#40;ms * sizeof **a&#41;; /*Here, sizeof&#40;**p&#41; is same as sizeof&#40;int&#41; */
         b&#91;i&#93; = malloc&#40;ms * sizeof **b&#41;;
         c&#91;i&#93; = malloc&#40;ms * sizeof **c&#41;;
         if&#40;a&#91;i&#93; == NULL ||b&#91;i&#93; == NULL ||c&#91;i&#93; == NULL&#41;
         &#123;
             printf&#40;"Memory allocation failed. Exiting...."&#41;; // return 1;
		 &#125;
    &#125;
  &#125;
  else
  &#123;
        printf&#40;"Memory allocation failed. Exiting...."&#41;; // return 1;
  &#125;/*~~~~~~~~~~~ end memory allocation master~~~~~*/
   /*~~~~~~~~~~~~~ fill in a  matrix with random numbs~~~~~~~~~~*/
   for &#40;i=0; i<ms; i++&#41;&#123;
     for &#40;j=0; j<ms; j++&#41;&#123;
       a&#91;i&#93;&#91;j&#93;= rand&#40;&#41; %10;
       printf&#40;"%d\t", a&#91;i&#93;&#91;j&#93;&#41;;&#125;
     printf&#40;"\n"&#41;;&#125;
   /*~~~~~~~~~~~~~ fill in b matrix with random numbs~~~~~~~~~~*/
   for &#40;i=0; i<ms; i++&#41;&#123;
    for &#40;j=0; j<ms; j++&#41;&#123;
      b&#91;i&#93;&#91;j&#93;= rand&#40;&#41; %10;
      printf&#40;"%d\t", b&#91;i&#93;&#91;j&#93;&#41;;&#125;
    printf&#40;"\n"&#41;;&#125;
  /* send matrix data to the worker tasks */
  averow = ms/numworkers;
  extra = ms%numworkers;
  offset = 0;
  mtype = FROM_MASTER;
  for &#40;dest=1; dest<=numworkers; dest++&#41; &#123;			
    rows = &#40;dest <= extra&#41; ? averow+1 &#58; averow;   	
    printf&#40;"   sending %d rows to task %d\n",rows,dest&#41;;
    MPI_Send&#40;&offset, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD&#41;;
    MPI_Send&#40;&rows, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD&#41;;
    count = rows*ms;
    MPI_Send&#40;&a&#91;offset&#93;&#91;0&#93;, count, MPI_INT, dest, mtype, MPI_COMM_WORLD&#41;;
    count = ms*ms;
    MPI_Send&#40;&b, count, MPI_INT, dest, mtype, MPI_COMM_WORLD&#41;;

    offset = offset + rows;
    &#125;

  /* wait for results from all worker tasks */
  mtype = FROM_WORKER;
  for &#40;i=1; i<=numworkers; i++&#41;	&#123;			
    source = i;
    MPI_Recv&#40;&offset, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status&#41;;
    MPI_Recv&#40;&rows, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status&#41;;
    count = rows*ms;
    MPI_Recv&#40;&c&#91;offset&#93;&#91;0&#93;, count, MPI_INT, source, mtype, MPI_COMM_WORLD, 
               &status&#41;;
 
    &#125;

  /* print results */
  printf&#40;"Here is the result matrix\n"&#41;;
  for &#40;i=0; i<ms; i++&#41; &#123; 
    printf&#40;"\n"&#41;; 
    for &#40;j=0; j<ms; j++&#41; 
      printf&#40;"%d\t  ", c&#91;i&#93;&#91;j&#93;&#41;;
    &#125;
  printf &#40;"\n"&#41;;

  &#125;  /* end of master section */



/**************************** worker task ************************************/
if &#40;taskid > MASTER&#41; &#123;
  mtype = FROM_MASTER;
  source = MASTER;
  printf &#40;"Master =%d, mtype=%d\n", source, mtype&#41;;
  MPI_Recv&#40;&offset, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status&#41;;
  printf &#40;"offset =%d\n", offset&#41;;
  MPI_Recv&#40;&rows, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status&#41;;
  printf &#40;"row =%d\n", rows&#41;;
  count = rows*ms;
/*~~~~~~~~~~~~~~~~~~~~~~~~memory allocation worker~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  a = malloc&#40;rows * sizeof&#40;*a&#41;&#41;;
  b = malloc&#40;ms * sizeof&#40;*b&#41;&#41;;
  c = malloc&#40;ms * sizeof&#40;*c&#41;&#41;;
if&#40;a != NULL||b!= NULL ||c != NULL&#41;
 &#123;
  for&#40;i = 0; i < ms; i++&#41; /* Loop through each row pointer to allocate memory for columns*/
    &#123;
         /* Set p&#91;i&#93; pointer to a block of memory for 'column' number of integers */
         b&#91;i&#93; = malloc&#40;ms * sizeof **b&#41;;/*Here, sizeof&#40;**p&#41; is same as sizeof&#40;int&#41; */
         c&#91;i&#93; = malloc&#40;ms * sizeof **c&#41;;
         if&#40;b&#91;i&#93; == NULL ||c&#91;i&#93; == NULL&#41;
         &#123;
             printf&#40;"Memory allocation failed. Exiting...."&#41;; // return 1;
 	     &#125;
    &#125;

  for&#40;i = 0; i < rows; i++&#41; /* Loop through each row pointer to allocate memory for columns*/
    &#123;
         /* Set p&#91;i&#93; pointer to a block of memory for 'column' number of integers */
         a&#91;i&#93; = malloc&#40;ms * sizeof **a&#41;; /*Here, sizeof&#40;**p&#41; is same as sizeof&#40;int&#41; */
         if&#40;a&#91;i&#93; == NULL &#41;
         &#123;
             printf&#40;"Memory allocation failed. Exiting...."&#41;; // return 1;
         &#125;
    &#125;
 &#125;
else
 &#123;
        printf&#40;"Memory allocation failed. Exiting...."&#41;; // return 1; 
 &#125;
/*~~~~~~~~~~~ end memory allocation worker~~~~~*/

  MPI_Recv&#40;&a, count, MPI_INT, source, mtype, MPI_COMM_WORLD, &status&#41;;
  printf &#40;"a&#91;0&#93;&#91;0&#93; =%e\n", a&#91;0&#93;&#91;0&#93;&#41;;
  count = ms*ms;
  MPI_Recv&#40;&b, count, MPI_INT, source, mtype, MPI_COMM_WORLD, &status&#41;;
  printf &#40;"b=\n"&#41;;
  for &#40;k=0; k<ms; k++&#41;
    for &#40;i=0; i<rows; i++&#41; &#123;
      c&#91;i&#93;&#91;k&#93; = 0;
      for &#40;j=0; j<ms; j++&#41;
        c&#91;i&#93;&#91;k&#93; = c&#91;i&#93;&#91;k&#93; + a&#91;i&#93;&#91;j&#93; * b&#91;j&#93;&#91;k&#93;;
      &#125;

  mtype = FROM_WORKER;
  printf &#40;"after computer\n"&#41;;
  MPI_Send&#40;&offset, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD&#41;;
  MPI_Send&#40;&rows, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD&#41;;
  MPI_Send&#40;&c, rows*ms, MPI_INT, MASTER, mtype, MPI_COMM_WORLD&#41;;
  printf &#40;"after send\n"&#41;;

  &#125;  /* end of worker */
  MPI_Finalize&#40;&#41;;
&#125; /* of main */
[/code]

alex599
Δημοσιεύσεις: 66
Εγγραφή: 17 Δεκ 2008 01:11
Τοποθεσία: Πάτρα

MPI malloc

Δημοσίευση από alex599 » 28 Ιουν 2010 12:20

Δεν κάνεις σωστά το memory allocation με την malloc.. Θα πρέπει να αλλάξεις και τον τύπο. Γενικά ένας δισσιάστατος πίνακας n*n δημιουργείται με έναν τέτοιο κώδικα:

Κώδικας: Επιλογή όλων

a=&#40;int *&#41;malloc&#40;n*sizeof&#40;int&#41;&#41;;
for &#40;i=0; i<n; i++&#41;&#123;
   a&#91;i&#93;=&#40;int **&#41;malloc&#40;n*sizeof&#40;*int&#41;&#41;;
&#125;
while(!dead()) ++knowledge;

JinTonic
Δημοσιεύσεις: 9
Εγγραφή: 11 Ιουν 2010 15:18

MPI malloc

Δημοσίευση από JinTonic » 28 Ιουν 2010 17:49

Ευχαριστώ για την ανταποκριση...
αλλαξα τα pointers και απο **α τα εκανα *α γιατι το MPI δεν δεχετε τετοιο τυπο δεδομενων και εκανα ολες τισ απαρετητεσ αλλαγες αλλα ακομα δεν δουλευει...
το προγραμμα σταματαει οταν κανω malloc ston worker του MPI. Μηπως εχει κανεις καμια ιδεα??


[electra:12013] *** Process received signal ***
[electra:12013] Signal: Segmentation fault (11)
[electra:12013] Signal code: Address not mapped (1)
[electra:12013] Failing at address: 0x600000003
offset =0
row =4
--------------------------------------------------------------------------
mpiexec noticed that process rank 1 with PID 12013 on node electra.it.uom.gr exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------

Κώδικας: Επιλογή όλων


int *a, *b, *c;

MPI_Init&#40;&argc, &argv&#41;;
MPI_Comm_rank&#40;MPI_COMM_WORLD, &taskid&#41;;
MPI_Comm_size&#40;MPI_COMM_WORLD, &numtasks&#41;;
numworkers = numtasks-1;

/**************************** master task ***********************************/
if &#40;taskid == MASTER&#41; &#123;
  printf&#40;"Number of worker tasks = %d\n",numworkers&#41;; 
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~memory allocation master ~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  a = malloc&#40;ms * ms * sizeof&#40;int&#41;&#41;;
  b = malloc&#40;ms * ms * sizeof&#40;int&#41;&#41;;
  c = malloc&#40;ms * ms * sizeof&#40;int&#41;&#41;;
   
   for &#40;i=0; i<ms; i++&#41;&#123;
     for &#40;j=0; j<ms; j++&#41;&#123;
       a&#91;i*ms+j&#93;= rand&#40;&#41; %10;
       printf&#40;"%d\t", a&#91;i*ms+j&#93;&#41;;&#125;
     printf&#40;"\n"&#41;;&#125;
   for &#40;i=0; i<ms; i++&#41;&#123;
    for &#40;j=0; j<ms; j++&#41;&#123;
      b&#91;i*ms+j&#93;= rand&#40;&#41; %10;
      printf&#40;"%d\t", b&#91;i*ms+j&#93;&#41;;&#125;
    printf&#40;"\n"&#41;;&#125;
  /* send matrix data to the worker tasks */
  averow = ms/numworkers;
  extra = ms%numworkers;
  offset = 0;
  mtype = FROM_MASTER;
  for &#40;dest=1; dest<=numworkers; dest++&#41; &#123;			
    rows = &#40;dest <= extra&#41; ? averow+1 &#58; averow;   	
    printf&#40;"   sending %d rows to task %d\n",rows,dest&#41;;
    MPI_Send&#40;&offset, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD&#41;;
    MPI_Send&#40;&rows, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD&#41;;
    count = rows*ms;
    MPI_Send&#40;&a&#91;offset*ms&#93;, count, MPI_INT, dest, mtype, MPI_COMM_WORLD&#41;;
    count = ms*ms;
    MPI_Send&#40;&b, count, MPI_INT, dest, mtype, MPI_COMM_WORLD&#41;;
    offset = offset + rows;
    &#125;
  /* wait for results from all worker tasks */
 //more code here
  &#125;  /* end of master section */

/**************************** worker task ************************************/
if &#40;taskid > MASTER&#41; &#123;
  mtype = FROM_MASTER;
  source = MASTER;
  printf &#40;"Master =%d, mtype=%d\n", source, mtype&#41;;
  MPI_Recv&#40;&offset, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status&#41;;
  printf &#40;"offset =%d\n", offset&#41;;
  MPI_Recv&#40;&rows, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status&#41;;
  printf &#40;"row =%d\n", rows&#41;;
  count = rows*ms;
/*~~~~~~~~~~~~~~~~~~~~~~~~memory allocation worker~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  a = &#40;int *&#41; malloc&#40;rows * ms * sizeof&#40;int&#41;&#41;;
  b = &#40;int *&#41; malloc&#40;ms * ms * sizeof&#40;int&#41;&#41;;
  c = &#40;int *&#41; malloc&#40;ms * rows * sizeof&#40;int&#41;&#41;;
/*~~~~~~~~~~~ end memory allocation worker~~~~~*/

  MPI_Recv&#40;&a, count, MPI_INT, source, mtype, MPI_COMM_WORLD, &status&#41;;
  printf &#40;"a&#91;0&#93; =%e\n", a&#91;0&#93;&#41;;
  count = ms*ms;
  MPI_Recv&#40;&b, count, MPI_INT, source, mtype, MPI_COMM_WORLD, &status&#41;;
  printf &#40;"b=\n"&#41;;
 

Απάντηση

Επιστροφή στο “C, C++”

Μέλη σε σύνδεση

Μέλη σε αυτήν τη Δ. Συζήτηση: Δεν υπάρχουν εγγεγραμμένα μέλη και 0 επισκέπτες