fread() в MPI выдает ошибку шины Signal 7

#c #mpi #parallel-processing #openmpi

#c #mpi #параллельная обработка #openmpi

Вопрос:

Я новичок в C и MPI. У меня есть следующий код, который я использую с MPI.

 #include "RabinKarp.c"
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<math.h>
#include </usr/include/mpi/mpi.h>

typedef struct {
    int lowerOffset;
    int upperOffset;
    int processorNumber;

} patternPartitioning;

int rank;
FILE *fp;

char* filename = "/home/rohit/Downloads/10_seqs_2000_3000_bp.fasta";
int n = 0;
int d = 0;
//number of processors
int k, i = 0, lower_limit, upper_limit;

int main(int argc, char** argv) {
    char* pattern= "taaat";
    patternPartitioning partition[k];
        MPI_Init(amp;argc, amp;argv);
        MPI_Comm_size(MPI_COMM_WORLD, amp;k);
        MPI_Comm_rank(MPI_COMM_WORLD, amp;rank);


    fp = fopen(filename, "rb");
    if (fp != '') {
        fseek(fp, 0L, SEEK_END);
        n = ftell(fp);
        fseek(fp, 0L, SEEK_SET);
    }


    //Do for Master Processor
    if(rank ==0){
    int m = strlen(pattern);
    printf("pattern length is %d n", m);

    d = (int)(n - m   1) / k;
        for (i = 0; i <= k - 2; i  ) {
            lower_limit = round(i * d);
            upper_limit = round((i   1) * d)   m - 1;
            partition->lowerOffset = lower_limit;
            partition->upperOffset = upper_limit;
            partition->processorNumber = i 1;

            // k-2 times calculate the limits like this
            printf(" the lower limit is %d and upper limit is%dn",
                    partition->lowerOffset, partition->upperOffset);
            int mpi_send_block[2];
            mpi_send_block[0]= lower_limit;
            mpi_send_block[1] = upper_limit;
            MPI_Send(mpi_send_block, 2, MPI_INT, i 1, i 1, MPI_COMM_WORLD);

            //int MPI_Send(void *buf, int count, MPI_Datatype dtype, int dest, int tag, MPI_Comm comm);
        }
        // for the last processor calculate the index here
        lower_limit = round((k - 1) * d);
        upper_limit = n;
        partition->lowerOffset = lower_limit;
        partition->upperOffset = n;
        partition->processorNumber = k;

        printf("Processor : %d : has start : %d  : and end : %d :n",rank,partition->lowerOffset,partition->upperOffset);

        //perform the search here

        int size = partition->upperOffset-partition->lowerOffset;
        char *text = (char*) malloc (size);

fseek(fp,partition->lowerOffset , SEEK_SET);
fread(amp;text, sizeof(char), size, fp);
printf("read in rank0");
 fputs(text,stdout);


                int number =0;
 fputs(text,stdout);
fputs(pattern,stdout);
         number = rabincarp(text,pattern);
        for (i = 0; i <= k - 2; i  ) {
            int res[1];
            res[0]=0;
            MPI_Status status;
         // MPI_Recv(res, 1, MPI_INT, i 1, i 1, MPI_COMM_WORLD, amp;status);
        //  number = number   res[0];
        }
        printf("nntotal number of result found:%dn", number);


    } else {
        patternPartitioning mypartition;
        MPI_Status status;
        int number[1];
        int mpi_recv_block[2];
        MPI_Recv(mpi_recv_block, 2, MPI_INT, 0, rank, MPI_COMM_WORLD,
                amp;status);
        printf("Processor : %d : has start : %d  : and end : %d :n",rank,mpi_recv_block[0],mpi_recv_block[1]);

        //perform the search here

        int size = mpi_recv_block[1]-mpi_recv_block[0];
       char *text = (char*) malloc (size);
            fseek(fp,mpi_recv_block[0] , SEEK_SET);
        fread(amp;text, sizeof(char), size, fp);
printf("read in rank1");

     //  fread(text,size,size,fp);
       printf("length of text segment by proc: %d is %d",rank,(int)strlen(text));

         number[0] = rabincarp(text,pattern);
        //MPI_Send(number, 1, MPI_INT, 0, rank, MPI_COMM_WORLD);
    }

MPI_Barrier(MPI_COMM_WORLD);
    MPI_Finalize();
fclose(fp);
    return (EXIT_SUCCESS);
}
  

если я запускаю ( mpirun -np 2 pnew) это, я получаю следующую ошибку:

   [localhost:03265] *** Process received signal ***
[localhost:03265] *** Process received signal ***
--------------------------------------------------------------------------
mpirun noticed that process rank 1 with PID 3265 on node localhost exited on signal 7 (Bus error).
  

итак, если я удалю инструкции fread (), я не получу ошибку .. кто-нибудь может сказать мне, чего мне не хватает?

Ответ №1:

 char *text = (char*) malloc (size);

fseek(fp,partition->lowerOffset , SEEK_SET);
fread(amp;text, sizeof(char), size, fp);
  

В документации для fread говорится, что «Функция fread() считывает nmemb элементы данных, каждый size байт длиной, из потока, на который указывает stream , сохраняя их в местоположении, указанном ptr «.

Поскольку text это a char * , amp;text это адрес a char * . В нем будет недостаточно места для хранения данных, которые вы читаете. Вы хотите передать fread адрес выделенной вами памяти, а не адрес переменной, содержащей этот адрес! (Поэтому удалите amp; .)

Ответ №2:

 if (fp != '') {
  

fp — это ФАЙЛ * , ‘ 0’ — это константа int.
Это не та ошибка, но я предлагаю вам скомпилировать с более высоким уровнем предупреждения для обнаружения ошибок такого рода.