Ошибка сегментации с использованием MPI - PullRequest
0 голосов
/ 24 мая 2019

У меня есть матрица с числами, которую я хочу распределить между процессами MPI (я хочу разделить матрицу на куски и сделать так, чтобы каждый процесс выполнял свою часть). Вы можете видеть, что я прокомментировал это ниже, чтобы вы поняли:

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MASTER 0

int *data; // pointer to data
int total_elems; // total number of elems
int type_size; // size of the data type (in our case it will be int)
int main (int argc, char *argv[]){
        MPI_Status status; 
        int my_rank,my_size; 
        int rc = -1; 
        int chunk; 
        int i; 

        type_size = sizeof(int);
        if (argc != 2){ 
                printf("usage: %s file_name\n",argv[0]); 
                exit(1); 
        }
        printf("Using %s as input\n",argv[1]); 
        total_elems = file_size(argv[1],sizeof(int)); // this function calculates the number of elems in the file/matrix
        if (total_elems<0){ 
                printf("Invalid number of elements\n"); 
                MPI_Abort(MPI_COMM_WORLD, rc); 
        }
        printf("There are %d elems in the matrix\n",total_elems); 

        MPI_Init(&argc,&argv); // initialize MPI environment
        MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); 
        MPI_Comm_size(MPI_COMM_WORLD,&my_size); 

        printf("Number of MPI processes: %d\n", my_size);
        chunk = total_elems/my_size; // elems in a chunk, a chunk for every process
        printf("Chunk size: %d\n", chunk); 
printf("up to here ok 0\n");
if (my_rank == MASTER) 
        {
             // nothing  
printf("up to here ok 0.5\n"); 
         // here it is still ok
        }else{  // NOW HERE ATTENTION!! here it is still ok
                printf("ok?\n"); // does not print this printf, i have a segmentation fault
                data = (int *)malloc(chunk*sizeof(int)); // we assign the number of bytes to the chunk 
                if (data == NULL){
                        printf("Error in malloc\n");
                        MPI_Abort(MPI_COMM_WORLD, rc); 
                }

                rc = read_from_pos(argv[1], chunk*(my_rank-1), chunk, type_size, (void *)data); // a function that reads from the file from a position a specified number of elements and stores in the buffer data
                if (rc<0){ 
                        printf("Error reading file\n");
                        MPI_Abort(MPI_COMM_WORLD, rc);
                }


        }

И функция 'read_from_pos':

int read_from_pos(char *name, uint pos,uint num_elems,uint type_size,void *buff)
{
        int fd,ret=0,pending,ready=0;
        fd=open(name,O_RDONLY); 
        if (fd<0)  return -1;
        ret=lseek(fd,(pos*type_size),SEEK_SET); // we go to the position
        if (ret!=pos*type_size) return -1;
        pending=num_elems*type_size; // pending items to be read
        ready=0; // read bytes 0 at the moment
        while(pending>0){ 
                ret=read(fd,(char *)buff+ready,pending);
                if (ret<0) return -1;
                pending=pending-ret;
                ready=ready+ret;
        }
        printf("Total number of elements read %u\n",ready/type_size);
        close(fd);
        return 0;
}

Итак, я не понимаю, почему в операторе if все по-прежнему нормально, но затем, когда он переходит к оператору else, когда процесс не является главным, он даже не печатает printf и получает ошибку сегментации , Я уверен, что printf не может выдать ошибку сегментации, я использовал "\ n", поэтому я думаю, что это malloc, но все же я думаю, что все в порядке. Я назначил размер куска в malloc для каждого процесса.

Так что, возможно, это из-за MPI. Должен ли я как-то защитить этот кусок кода? Я хочу независимый кусок матрицы для каждого подчиненного процесса, я ошибаюсь?

На выходе с матрицей из 10 чисел я получаю это:

./prog easy.txt
Using easy.txt as input
There are 10 elements in the file
There are 10 elems in the matrix
Number of MPI processes: 1
Chunk size: 10
up to here ok 0
up to here ok 0.5
Segmentation fault
...