Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing to text file in MPI

I'm trying to write to text file with MPI but the file is not created. I need only to write at the master (rank = 0), but nothing works. It only working when I running the program in console (and save corrupt element) and not in Mpich2 and I attached the code. Thanks for helping.

  /* -*- Mode: C; c-basic-offset:4 ; -*- */
/*
 *  (C) 2001 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

/* This is an interactive version of cpi */
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>


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

    int  namelen, numprocs, rank;
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);    
    MPI_Get_processor_name(processor_name,&namelen);
    MPI_Status status;
    FILE* f = fopen("test.txt","wb+");

    if (rank == 0) {
        for (int i=0; i < 5; i++){
            fprintf(f,"%d \n",i);
        }
        fclose(f);
    }
    else {
        // do nothing
    }


    MPI_Finalize();
    return 0;
}
like image 794
Ofir N Avatar asked Sep 26 '22 10:09

Ofir N


1 Answers

In the sample code you posted, all processes open the file and only process 0 closes it. Could you try the following modification ?

if (rank == 0) {
    FILE* f = fopen("test.txt","wb+");
    if(f==NULL){printf("failed to open file: permission issue ?\n");exit(1);}
    for (int i=0; i < 5; i++){
        fprintf(f,"%d \n",i);
    }
    fclose(f);
}

Since your code seems to come from the Argonne National Laboratory, I suppose that it is ran on a cluster using a particular file system. The following code is based on yours. It makes use of MPI_File_open() and MPI_File_write() on a single process, using MPI_COMM_SELF.

/* This is an interactive version of cpi */
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


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

    int  namelen, numprocs, rank;
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);    
    MPI_Get_processor_name(processor_name,&namelen);
    MPI_Status status;

    MPI_File fh;


    if (rank == 0) {
        MPI_File_open(MPI_COMM_SELF, "test.txt",MPI_MODE_CREATE | MPI_MODE_WRONLY,MPI_INFO_NULL,&fh);
        //FILE* f = fopen("test.txt","wb+");
        //if(f==NULL){
        //printf("failed to open file\n");exit(1);
        //}
        for (int i=0; i < 5; i++){
            char buf[42];
            //fprintf(f,"%d \n",i);
            snprintf(buf,42,"%d \n",i);
            MPI_File_write(fh,buf,strlen(buf), MPI_CHAR,&status);
        }
        //        fclose(f);
        MPI_File_close(&fh);
    }
    else {
        // do nothing
    }


    MPI_Finalize();
    return 0;
}

Please make sure that you hae the permission to write in the considered folder. Make sure that all nodes can access this folder ! Try some folders like your folder in /tmp or /scratch... Your cluster may have some sort of documentation somewhere telling you where you can write files !

like image 133
francis Avatar answered Oct 12 '22 11:10

francis