writing a matrix into a single txt file with mpi

前端 未结 1 1858
天命终不由人
天命终不由人 2020-12-01 22:39

I have a huge matrix that I divided it into some sub matrices and I make some computation on it. After those computations I have to write that matrix into a single file for

1条回答
  •  情话喂你
    2020-12-01 22:58

    So it's not a good idea to write large amounts of data as text. It's really, really, slow, it generates unnecessarily large files, and it's a pain to deal with. Large amounts of data should be written as binary, with only summary data for humans written as text. Make the stuff the computer is going to deal with easy for the computer, and only the stuff you're actually going to sit down and read easy for you to deal with (eg, text).

    Whether you're going to write as text or binary, you can use MPI-IO to coordinate your output to the file to generate one large file. We have a little tutorial on the topic (using MPI-IO, HDF5, and NetCDF) here. For MPI-IO, the trick is to define a type (here, a subarray) to describe the local layout of data in terms of the global layout of the file, and then write to the file using that as the "view". Each file sees only its own view, and the MPI-IO library coordinates the output so that as long as the views are non-overlapping, everything comes out as one big file.

    If we were writing this out in binary, we'd just point MPI_Write to our data and be done with it; since we're using text, we have to convert out data into a string. We define our array the way we normally would have, except instead of it being of MPI_FLOATs, it's of a new type which is charspernum characters per number.

    The code follows:

    #include 
    #include 
    #include 
    #include 
    
    float **alloc2d(int n, int m) {
        float *data = malloc(n*m*sizeof(float));
        float **array = malloc(n*sizeof(float *));
        for (int i=0; i

    Running gives:

    $ mpicc -o matrixastxt matrixastxt.c  -std=c99
    $ mpirun -np 4 ./matrixastxt
    $ more all-data.txt 
       0.000    0.000    0.000    0.000    0.000    0.000    0.000    0.000    0.000    0.000
       0.000    0.000    0.000    0.000    0.000    0.000    0.000    0.000    0.000    0.000
       1.000    1.000    1.000    1.000    1.000    1.000    1.000    1.000    1.000    1.000
       1.000    1.000    1.000    1.000    1.000    1.000    1.000    1.000    1.000    1.000
       2.000    2.000    2.000    2.000    2.000    2.000    2.000    2.000    2.000    2.000
       2.000    2.000    2.000    2.000    2.000    2.000    2.000    2.000    2.000    2.000
       3.000    3.000    3.000    3.000    3.000    3.000    3.000    3.000    3.000    3.000
       3.000    3.000    3.000    3.000    3.000    3.000    3.000    3.000    3.000    3.000
       3.000    3.000    3.000    3.000    3.000    3.000    3.000    3.000    3.000    3.000
       3.000    3.000    3.000    3.000    3.000    3.000    3.000    3.000    3.000    3.000
    

    0 讨论(0)
提交回复
热议问题