MPI 中的 fread() 给出信号 7 总线错误

发布于 2024-12-09 15:29:59 字数 4305 浏览 0 评论 0原文

我是 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(&argc, &argv);
        MPI_Comm_size(MPI_COMM_WORLD, &k);
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);


    fp = fopen(filename, "rb");
    if (fp != '\0') {
        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%d\n",
                    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(&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, &status);
        //  number = number + res[0];
        }
        printf("\n\ntotal number of result found:%d\n", 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,
                &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(&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() 语句,我不会收到错误..任何人都可以告诉我我错过了什么吗?

I am a newbie to C and MPI.
I have the following code which I am using with 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(&argc, &argv);
        MPI_Comm_size(MPI_COMM_WORLD, &k);
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);


    fp = fopen(filename, "rb");
    if (fp != '\0') {
        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%d\n",
                    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(&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, &status);
        //  number = number + res[0];
        }
        printf("\n\ntotal number of result found:%d\n", 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,
                &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(&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);
}

if I run( mpirun -np 2 pnew ) this I am getting the following error:

  [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).

so if I remove the fread() statements I dont get the error.. can anyone tell me what am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安人多梦 2024-12-16 15:29:59
char *text = (char*) malloc (size);

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

fread 的文档表示“函数 fread() 读取数据的 nmemb 元素,每个 size 字节长,来自 stream 指向的流,将它们存储在 ptr 指定的位置。”

由于 textchar *,因此 &textchar * 的地址。那将没有足够的空间来保存您正在读取的数据。您想要传递fread您分配的内存的地址,而不是保存该地址的变量的地址! (因此删除 &。)

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

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

The documentation for fread says "The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr."

Since text is a char *, &text is the address of a char *. That won't have enough space to hold the data you're reading. You want to pass fread the address of the memory you allocated, not the address of the variable holding that address! (So remove the &.)

那些过往 2024-12-16 15:29:59
if (fp != '\0') {

fp 是 FILE* , '\0' 是 int 常量。
这不是错误,但我建议您使用更高的警告级别进行编译以捕获此类错误。

if (fp != '\0') {

fp is FILE* , '\0' is an int constant.
This is not the error, but I suggest you compile with a higher warning level to catch this kind of errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文