大家好,我正在学习使用MPI编程,遇到了这个问题。可以说我有一个.txt文件,其中包含3000000行/行的文本(这是一个字典),所以它将是数组字符串,如何将它们分块以便由4个处理器处理?即我要让处理器0负责1-750 000行的处理,让处理器1负责750 001-1 500 000等。然后,每个过程获取部分数据,并放入一个函数中,例如find_some_word。

现在我明白了,但我不知道该如何从我的文章中获得排名的一个字。这部分在程序中有注释,怎么做

#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <conio.h>

bool if_anagram(char *a, char *b)
{

    int dl1 = strlen(a), dl2 = strlen(b);


    if(dl1!=dl2) return false;
    if(strcmp(a,b) == 0) return false;

    int licz[0x100]={};

    for(int i=0;i<dl1;i++)
        licz[(unsigned char) a[i]]++;
    for(int i=0;i<dl1;i++)
        licz[(unsigned char) b[i]]--;
    for(int i=0;i<256;i++)
        if(licz[i]!=0)
            return false;

  return true;
}

void parprocess(MPI_File *in, char *a, const int rank, const int size, const int overlap) {

    MPI_Offset globalstart;
    int mysize;
    char *chunk;

    /* read in relevant chunk of file into "chunk",
     * which starts at location in the file globalstart
     * and has size mysize
     */
    {
        MPI_Offset globalend;
        MPI_Offset filesize;

        /* figure out who reads what */
        MPI_File_get_size(*in, &filesize);
        filesize--;  /* get rid of text file eof */
        mysize = filesize/size;
        globalstart = rank * mysize;
        globalend   = globalstart + mysize - 1;
        if (rank == size-1) globalend = filesize-1;

        /* add overlap to the end of everyone's chunk except last proc... */
        if (rank != size-1)
            globalend += overlap;

        mysize =  globalend - globalstart + 1;

        /* allocate memory */
        chunk = malloc( (mysize + 1)*sizeof(char));

        /* everyone reads in their part */
        MPI_File_read_at_all(*in, globalstart, chunk, mysize, MPI_CHAR, MPI_STATUS_IGNORE);
        chunk[mysize] = '\0';
    }


    /*
     * everyone calculate what their start and end *really* are by going
     * from the first newline after start to the first newline after the
     * overlap region starts (eg, after end - overlap + 1)
     */

    int locstart=0, locend=mysize-1;
    if (rank != 0) {
        while(chunk[locstart] != '\n') locstart++;
        locstart++;
    }
    if (rank != size-1) {
        locend-=overlap;
        while(chunk[locend] != '\n') locend++;
    }
    mysize = locend-locstart+1;

    /* "Process" our chunk by replacing non-space characters with '1' for
     * rank 1, '2' for rank 2, etc...
     */

    char b[101];
    for (int i=locstart; i<=locend; i++) {
                                                // THIS PART
        //fscanf (chunk,"%s",b);                // how get a single word from chunk
        //if(if_anagram(a,b)){
            //printf("Word %s is anagram!\n", b);

    //  It is orginal part
    //    char c = chunk[i];
     //   chunk[i] = ( isspace(c) ? c : '1' + (char)rank );
    //    printf("Wyraz %d\n", rank);
        }
    //}

}

int main(int argc, char **argv) {

    MPI_File in;
    int rank, size;
    int ierr;
    const int overlap = 100;
    char a[101];


    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    if (argc != 2) {
        if (rank == 0) fprintf(stderr, "Usage: %s infilename\n", argv[0]);
        MPI_Finalize();
        exit(1);
    }

    ierr = MPI_File_open(MPI_COMM_WORLD, argv[1], MPI_MODE_RDONLY, MPI_INFO_NULL, &in);
    if (ierr) {
        if (rank == 0) fprintf(stderr, "%s: Couldn't open file %s\n", argv[0], argv[1]);
        MPI_Finalize();
        exit(2);
    }

        printf("Put word: ");
        scanf ("%s", a);
    printf("\n");

    parprocess(&in, a, rank, size, overlap);


    MPI_File_close(&in);
    MPI_Finalize();
    return 0;
}

最佳答案

关于I / O的很好的教程:
https://www.tacc.utexas.edu/documents/13601/900558/MPI-IO-Final.pdf/eea9d7d3-4b81-471c-b244-41498070e35d

然后,必须了解MPI-IO无法读取文件的内容:-)。它“仅”允许进程跳过文件的一部分(以字节为单位),如果您要跳过给定的行数并且每行大小不同,则除了读取整个文件以查找文件外,您没有其他解决方案。 '\ n'。 MPI-IO不是魔术师...

最好的解决方案是创建(一次)标题,该标题描述线组在何处结束。您可以获取每组10,000行的字节数,然后将其作为文件头。您将可以跳过一些块。显然要这样做,因此您必须一次读取整个文件...

10-08 08:24
查看更多