MPI_File_write C










0















I have to use MPI to compute value of concentration from a 3D matrix.



I divide my matrix in slices depending on the value of the number of proc I will use.



I get smaller 3D matrix which work independently but I want that the values of concentration they compute to be writen in the same file such as the 0 to n-1 lines are writen by proc 0, n to 2n-1 by proc 1,..



I know that i can use an offset in the mpi_file_write_at function to perform such a thing but i don't know how to implement it.



I know that an easy solution would be to ask proc 0 to open the file, write and close it then proc 1 opens.



Don't mind the code in itself as it is not finished



#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>

int main(int argc, char *argv)

char *name = argv[1];

double h, m, L, T_max, v_x, v_y, v_z, D, r_thresh;
unsigned int S;

FILE *file = fopen(name, "rb");


if(!file)

fprintf(stderr, "Not able to open the file %sn", name);

else
printf("Able to open the file %sn", name);

fscanf(file, "%lf", &h);
fscanf(file, "%lf", &m);
fscanf(file, "%lf", &L);
fscanf(file, "%lf", &T_max);
fscanf(file, "%lf", &v_x);
fscanf(file, "%lf", &v_y);
fscanf(file, "%lf", &v_z);
fscanf(file, "%lf", &D);
fscanf(file, "%u", &S);
fscanf(file, "%lf", &r_thresh);

fclose(file);

if(((L/h) % 2) != 0)
printf("Uncorrect parameters L and h ! n");
return -1;

else
unsigned int edge = (L/h);
unsigned int middle = edge/2;
unsigned int N = edge+1;

if(((T_max/m) % 1) != 0)
printf("Uncorrect parameters T_max and m ! n");
return -1;

else
unsigned int t_end = T_max/m;


unsigned int myrank, nbproc;
MPI_Init(&argc, &argv) ;
MPI_Comm_rank( MPI_COMM_WORLD , &myrank );
MPI_Comm_size( MPI_COMM_WORLD , &nbproc );

double w = N/nbproc;
unsigned int width = round(w);
unsigned int k_min, k_max;
k_min = myrank*width;
k_max = (myrank+1)*width-1;
width = width+2;
if(nbproc==1)
width = N;

else if(myrank==(nbproc-1))
k_max = N-1;
width = k_max-k_min+1;
width = width+1;

else if(myrank==0)
width = width-1;




double ***C ;
C = malloc(width * sizeof(*C));
for(k=0 ; k < width ; k++)
C[k] = malloc(N * sizeof(**C));

for(k=0 ; k < width ; k++)
for(j=0 ; j < N ; j++)
C[k][j] = malloc(N * sizeof(***C));




unsigned int i,j,k,n;
for(k=0 ; k<width ; k++)
for(j=0 ; j<N ; j++)
for(i=0 ; i<N ; i++)
C[k][j][i]=0;



if(middle>=k_min && middle=<k_max)
C[middle-k_min][middle][middle] = 1;



if(myrank==0









share|improve this question



















  • 2





    a Minimal, Complete, and Verifiable example is welcome ! simply put a comment instead of writing to the file.

    – Gilles Gouaillardet
    Nov 13 '18 at 21:32











  • do all the lines have the same and fixed length ?

    – Gilles Gouaillardet
    Nov 14 '18 at 20:45











  • Normally yes but anyway each process will know how many lines it has to write. @GillesGouaillardet

    – Vandeghen Renaud
    Nov 14 '18 at 22:28











  • what matters is how many bytes each task has to know, if you do not know that, this is gonna be complicated.

    – Gilles Gouaillardet
    Nov 14 '18 at 23:17











  • each task will handle [sizeof(MPI_DOUBLE)*(L/h)*(L/h)*depth of the slice]. I don't know if it matters for you but all our computing will be done on the cluster of my university so i'm pretty sure i'll have enough memory.

    – Vandeghen Renaud
    Nov 15 '18 at 8:01
















0















I have to use MPI to compute value of concentration from a 3D matrix.



I divide my matrix in slices depending on the value of the number of proc I will use.



I get smaller 3D matrix which work independently but I want that the values of concentration they compute to be writen in the same file such as the 0 to n-1 lines are writen by proc 0, n to 2n-1 by proc 1,..



I know that i can use an offset in the mpi_file_write_at function to perform such a thing but i don't know how to implement it.



I know that an easy solution would be to ask proc 0 to open the file, write and close it then proc 1 opens.



Don't mind the code in itself as it is not finished



#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>

int main(int argc, char *argv)

char *name = argv[1];

double h, m, L, T_max, v_x, v_y, v_z, D, r_thresh;
unsigned int S;

FILE *file = fopen(name, "rb");


if(!file)

fprintf(stderr, "Not able to open the file %sn", name);

else
printf("Able to open the file %sn", name);

fscanf(file, "%lf", &h);
fscanf(file, "%lf", &m);
fscanf(file, "%lf", &L);
fscanf(file, "%lf", &T_max);
fscanf(file, "%lf", &v_x);
fscanf(file, "%lf", &v_y);
fscanf(file, "%lf", &v_z);
fscanf(file, "%lf", &D);
fscanf(file, "%u", &S);
fscanf(file, "%lf", &r_thresh);

fclose(file);

if(((L/h) % 2) != 0)
printf("Uncorrect parameters L and h ! n");
return -1;

else
unsigned int edge = (L/h);
unsigned int middle = edge/2;
unsigned int N = edge+1;

if(((T_max/m) % 1) != 0)
printf("Uncorrect parameters T_max and m ! n");
return -1;

else
unsigned int t_end = T_max/m;


unsigned int myrank, nbproc;
MPI_Init(&argc, &argv) ;
MPI_Comm_rank( MPI_COMM_WORLD , &myrank );
MPI_Comm_size( MPI_COMM_WORLD , &nbproc );

double w = N/nbproc;
unsigned int width = round(w);
unsigned int k_min, k_max;
k_min = myrank*width;
k_max = (myrank+1)*width-1;
width = width+2;
if(nbproc==1)
width = N;

else if(myrank==(nbproc-1))
k_max = N-1;
width = k_max-k_min+1;
width = width+1;

else if(myrank==0)
width = width-1;




double ***C ;
C = malloc(width * sizeof(*C));
for(k=0 ; k < width ; k++)
C[k] = malloc(N * sizeof(**C));

for(k=0 ; k < width ; k++)
for(j=0 ; j < N ; j++)
C[k][j] = malloc(N * sizeof(***C));




unsigned int i,j,k,n;
for(k=0 ; k<width ; k++)
for(j=0 ; j<N ; j++)
for(i=0 ; i<N ; i++)
C[k][j][i]=0;



if(middle>=k_min && middle=<k_max)
C[middle-k_min][middle][middle] = 1;



if(myrank==0









share|improve this question



















  • 2





    a Minimal, Complete, and Verifiable example is welcome ! simply put a comment instead of writing to the file.

    – Gilles Gouaillardet
    Nov 13 '18 at 21:32











  • do all the lines have the same and fixed length ?

    – Gilles Gouaillardet
    Nov 14 '18 at 20:45











  • Normally yes but anyway each process will know how many lines it has to write. @GillesGouaillardet

    – Vandeghen Renaud
    Nov 14 '18 at 22:28











  • what matters is how many bytes each task has to know, if you do not know that, this is gonna be complicated.

    – Gilles Gouaillardet
    Nov 14 '18 at 23:17











  • each task will handle [sizeof(MPI_DOUBLE)*(L/h)*(L/h)*depth of the slice]. I don't know if it matters for you but all our computing will be done on the cluster of my university so i'm pretty sure i'll have enough memory.

    – Vandeghen Renaud
    Nov 15 '18 at 8:01














0












0








0








I have to use MPI to compute value of concentration from a 3D matrix.



I divide my matrix in slices depending on the value of the number of proc I will use.



I get smaller 3D matrix which work independently but I want that the values of concentration they compute to be writen in the same file such as the 0 to n-1 lines are writen by proc 0, n to 2n-1 by proc 1,..



I know that i can use an offset in the mpi_file_write_at function to perform such a thing but i don't know how to implement it.



I know that an easy solution would be to ask proc 0 to open the file, write and close it then proc 1 opens.



Don't mind the code in itself as it is not finished



#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>

int main(int argc, char *argv)

char *name = argv[1];

double h, m, L, T_max, v_x, v_y, v_z, D, r_thresh;
unsigned int S;

FILE *file = fopen(name, "rb");


if(!file)

fprintf(stderr, "Not able to open the file %sn", name);

else
printf("Able to open the file %sn", name);

fscanf(file, "%lf", &h);
fscanf(file, "%lf", &m);
fscanf(file, "%lf", &L);
fscanf(file, "%lf", &T_max);
fscanf(file, "%lf", &v_x);
fscanf(file, "%lf", &v_y);
fscanf(file, "%lf", &v_z);
fscanf(file, "%lf", &D);
fscanf(file, "%u", &S);
fscanf(file, "%lf", &r_thresh);

fclose(file);

if(((L/h) % 2) != 0)
printf("Uncorrect parameters L and h ! n");
return -1;

else
unsigned int edge = (L/h);
unsigned int middle = edge/2;
unsigned int N = edge+1;

if(((T_max/m) % 1) != 0)
printf("Uncorrect parameters T_max and m ! n");
return -1;

else
unsigned int t_end = T_max/m;


unsigned int myrank, nbproc;
MPI_Init(&argc, &argv) ;
MPI_Comm_rank( MPI_COMM_WORLD , &myrank );
MPI_Comm_size( MPI_COMM_WORLD , &nbproc );

double w = N/nbproc;
unsigned int width = round(w);
unsigned int k_min, k_max;
k_min = myrank*width;
k_max = (myrank+1)*width-1;
width = width+2;
if(nbproc==1)
width = N;

else if(myrank==(nbproc-1))
k_max = N-1;
width = k_max-k_min+1;
width = width+1;

else if(myrank==0)
width = width-1;




double ***C ;
C = malloc(width * sizeof(*C));
for(k=0 ; k < width ; k++)
C[k] = malloc(N * sizeof(**C));

for(k=0 ; k < width ; k++)
for(j=0 ; j < N ; j++)
C[k][j] = malloc(N * sizeof(***C));




unsigned int i,j,k,n;
for(k=0 ; k<width ; k++)
for(j=0 ; j<N ; j++)
for(i=0 ; i<N ; i++)
C[k][j][i]=0;



if(middle>=k_min && middle=<k_max)
C[middle-k_min][middle][middle] = 1;



if(myrank==0









share|improve this question
















I have to use MPI to compute value of concentration from a 3D matrix.



I divide my matrix in slices depending on the value of the number of proc I will use.



I get smaller 3D matrix which work independently but I want that the values of concentration they compute to be writen in the same file such as the 0 to n-1 lines are writen by proc 0, n to 2n-1 by proc 1,..



I know that i can use an offset in the mpi_file_write_at function to perform such a thing but i don't know how to implement it.



I know that an easy solution would be to ask proc 0 to open the file, write and close it then proc 1 opens.



Don't mind the code in itself as it is not finished



#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>

int main(int argc, char *argv)

char *name = argv[1];

double h, m, L, T_max, v_x, v_y, v_z, D, r_thresh;
unsigned int S;

FILE *file = fopen(name, "rb");


if(!file)

fprintf(stderr, "Not able to open the file %sn", name);

else
printf("Able to open the file %sn", name);

fscanf(file, "%lf", &h);
fscanf(file, "%lf", &m);
fscanf(file, "%lf", &L);
fscanf(file, "%lf", &T_max);
fscanf(file, "%lf", &v_x);
fscanf(file, "%lf", &v_y);
fscanf(file, "%lf", &v_z);
fscanf(file, "%lf", &D);
fscanf(file, "%u", &S);
fscanf(file, "%lf", &r_thresh);

fclose(file);

if(((L/h) % 2) != 0)
printf("Uncorrect parameters L and h ! n");
return -1;

else
unsigned int edge = (L/h);
unsigned int middle = edge/2;
unsigned int N = edge+1;

if(((T_max/m) % 1) != 0)
printf("Uncorrect parameters T_max and m ! n");
return -1;

else
unsigned int t_end = T_max/m;


unsigned int myrank, nbproc;
MPI_Init(&argc, &argv) ;
MPI_Comm_rank( MPI_COMM_WORLD , &myrank );
MPI_Comm_size( MPI_COMM_WORLD , &nbproc );

double w = N/nbproc;
unsigned int width = round(w);
unsigned int k_min, k_max;
k_min = myrank*width;
k_max = (myrank+1)*width-1;
width = width+2;
if(nbproc==1)
width = N;

else if(myrank==(nbproc-1))
k_max = N-1;
width = k_max-k_min+1;
width = width+1;

else if(myrank==0)
width = width-1;




double ***C ;
C = malloc(width * sizeof(*C));
for(k=0 ; k < width ; k++)
C[k] = malloc(N * sizeof(**C));

for(k=0 ; k < width ; k++)
for(j=0 ; j < N ; j++)
C[k][j] = malloc(N * sizeof(***C));




unsigned int i,j,k,n;
for(k=0 ; k<width ; k++)
for(j=0 ; j<N ; j++)
for(i=0 ; i<N ; i++)
C[k][j][i]=0;



if(middle>=k_min && middle=<k_max)
C[middle-k_min][middle][middle] = 1;



if(myrank==0






c mpi openmp mpi-io






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 18:59







Vandeghen Renaud

















asked Nov 13 '18 at 21:24









Vandeghen RenaudVandeghen Renaud

12




12







  • 2





    a Minimal, Complete, and Verifiable example is welcome ! simply put a comment instead of writing to the file.

    – Gilles Gouaillardet
    Nov 13 '18 at 21:32











  • do all the lines have the same and fixed length ?

    – Gilles Gouaillardet
    Nov 14 '18 at 20:45











  • Normally yes but anyway each process will know how many lines it has to write. @GillesGouaillardet

    – Vandeghen Renaud
    Nov 14 '18 at 22:28











  • what matters is how many bytes each task has to know, if you do not know that, this is gonna be complicated.

    – Gilles Gouaillardet
    Nov 14 '18 at 23:17











  • each task will handle [sizeof(MPI_DOUBLE)*(L/h)*(L/h)*depth of the slice]. I don't know if it matters for you but all our computing will be done on the cluster of my university so i'm pretty sure i'll have enough memory.

    – Vandeghen Renaud
    Nov 15 '18 at 8:01













  • 2





    a Minimal, Complete, and Verifiable example is welcome ! simply put a comment instead of writing to the file.

    – Gilles Gouaillardet
    Nov 13 '18 at 21:32











  • do all the lines have the same and fixed length ?

    – Gilles Gouaillardet
    Nov 14 '18 at 20:45











  • Normally yes but anyway each process will know how many lines it has to write. @GillesGouaillardet

    – Vandeghen Renaud
    Nov 14 '18 at 22:28











  • what matters is how many bytes each task has to know, if you do not know that, this is gonna be complicated.

    – Gilles Gouaillardet
    Nov 14 '18 at 23:17











  • each task will handle [sizeof(MPI_DOUBLE)*(L/h)*(L/h)*depth of the slice]. I don't know if it matters for you but all our computing will be done on the cluster of my university so i'm pretty sure i'll have enough memory.

    – Vandeghen Renaud
    Nov 15 '18 at 8:01








2




2





a Minimal, Complete, and Verifiable example is welcome ! simply put a comment instead of writing to the file.

– Gilles Gouaillardet
Nov 13 '18 at 21:32





a Minimal, Complete, and Verifiable example is welcome ! simply put a comment instead of writing to the file.

– Gilles Gouaillardet
Nov 13 '18 at 21:32













do all the lines have the same and fixed length ?

– Gilles Gouaillardet
Nov 14 '18 at 20:45





do all the lines have the same and fixed length ?

– Gilles Gouaillardet
Nov 14 '18 at 20:45













Normally yes but anyway each process will know how many lines it has to write. @GillesGouaillardet

– Vandeghen Renaud
Nov 14 '18 at 22:28





Normally yes but anyway each process will know how many lines it has to write. @GillesGouaillardet

– Vandeghen Renaud
Nov 14 '18 at 22:28













what matters is how many bytes each task has to know, if you do not know that, this is gonna be complicated.

– Gilles Gouaillardet
Nov 14 '18 at 23:17





what matters is how many bytes each task has to know, if you do not know that, this is gonna be complicated.

– Gilles Gouaillardet
Nov 14 '18 at 23:17













each task will handle [sizeof(MPI_DOUBLE)*(L/h)*(L/h)*depth of the slice]. I don't know if it matters for you but all our computing will be done on the cluster of my university so i'm pretty sure i'll have enough memory.

– Vandeghen Renaud
Nov 15 '18 at 8:01






each task will handle [sizeof(MPI_DOUBLE)*(L/h)*(L/h)*depth of the slice]. I don't know if it matters for you but all our computing will be done on the cluster of my university so i'm pretty sure i'll have enough memory.

– Vandeghen Renaud
Nov 15 '18 at 8:01













1 Answer
1






active

oldest

votes


















0














Each process in an MPI program has a local address space. File I/O changes that: a file is a globally accessible linear stream of bytes. You need to think about where in the global space each process is writing. A couple ways to do this:



  • sometimes you can just have each process write to a corresponding chunk of the file. rank zero writes to the first megabyte, rank 1 to the 2nd, and so on. Might be too simplistic for your 3d array.


  • For more sophisticated decomposition, you set a "file view": you construct an MPI datatype describing the layout of data on disk. in your case, MPI_TYPE_CREATE_SUBARRAY is probably what you want: a 3d global matrix and each process contributes some subaray to the global picture.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289713%2fmpi-file-write-c%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Each process in an MPI program has a local address space. File I/O changes that: a file is a globally accessible linear stream of bytes. You need to think about where in the global space each process is writing. A couple ways to do this:



    • sometimes you can just have each process write to a corresponding chunk of the file. rank zero writes to the first megabyte, rank 1 to the 2nd, and so on. Might be too simplistic for your 3d array.


    • For more sophisticated decomposition, you set a "file view": you construct an MPI datatype describing the layout of data on disk. in your case, MPI_TYPE_CREATE_SUBARRAY is probably what you want: a 3d global matrix and each process contributes some subaray to the global picture.






    share|improve this answer



























      0














      Each process in an MPI program has a local address space. File I/O changes that: a file is a globally accessible linear stream of bytes. You need to think about where in the global space each process is writing. A couple ways to do this:



      • sometimes you can just have each process write to a corresponding chunk of the file. rank zero writes to the first megabyte, rank 1 to the 2nd, and so on. Might be too simplistic for your 3d array.


      • For more sophisticated decomposition, you set a "file view": you construct an MPI datatype describing the layout of data on disk. in your case, MPI_TYPE_CREATE_SUBARRAY is probably what you want: a 3d global matrix and each process contributes some subaray to the global picture.






      share|improve this answer

























        0












        0








        0







        Each process in an MPI program has a local address space. File I/O changes that: a file is a globally accessible linear stream of bytes. You need to think about where in the global space each process is writing. A couple ways to do this:



        • sometimes you can just have each process write to a corresponding chunk of the file. rank zero writes to the first megabyte, rank 1 to the 2nd, and so on. Might be too simplistic for your 3d array.


        • For more sophisticated decomposition, you set a "file view": you construct an MPI datatype describing the layout of data on disk. in your case, MPI_TYPE_CREATE_SUBARRAY is probably what you want: a 3d global matrix and each process contributes some subaray to the global picture.






        share|improve this answer













        Each process in an MPI program has a local address space. File I/O changes that: a file is a globally accessible linear stream of bytes. You need to think about where in the global space each process is writing. A couple ways to do this:



        • sometimes you can just have each process write to a corresponding chunk of the file. rank zero writes to the first megabyte, rank 1 to the 2nd, and so on. Might be too simplistic for your 3d array.


        • For more sophisticated decomposition, you set a "file view": you construct an MPI datatype describing the layout of data on disk. in your case, MPI_TYPE_CREATE_SUBARRAY is probably what you want: a 3d global matrix and each process contributes some subaray to the global picture.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 30 '18 at 3:20









        Rob LathamRob Latham

        3,48411541




        3,48411541





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289713%2fmpi-file-write-c%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20