Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7826177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:17:41+00:00 2026-06-02T09:17:41+00:00

I’m trying to read in multiple files using MPI-IO in C. I’m following this

  • 0

I’m trying to read in multiple files using MPI-IO in C. I’m following this example : http://users.abo.fi/Mats.Aspnas/PP2010/examples/MPI/readfile1.c

However I’m reading in a matrix a doubles instead of a string of chars. Here is that implementation:

/*
Simple MPI-IO program that demonstrate parallel reading from a file.
Compile the program with 'mpicc -O2 readfile1.c -o readfile1'
*/

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

#define FILENAME "filename.dat"

double** ArrayAllocation() {
    int i;
    double** array2D;
    array2D= (double**) malloc(num_procs*sizeof(double*));
    for(i = 0; i < num_procs; i++) {
        twoDarray[i] = (double*) malloc(column_size*sizeof(double));
    }
    return array2D;
}

int main(int argc, char* argv[]) {
  int i, np, myid;
  int bufsize, nrchar;
  double *buf;          /* Buffer for reading */
  double **matrix = ArrayAllocation();
  MPI_Offset filesize;
  MPI_File myfile;    /* Shared file */ 
  MPI_Status status;  /* Status returned from read */

  /* Initialize MPI */
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  MPI_Comm_size(MPI_COMM_WORLD, &np);

  /* Open the files */
  MPI_File_open (MPI_COMM_WORLD, FILENAME, MPI_MODE_RDONLY,
         MPI_INFO_NULL, &myfile);

  /* Get the size of the file */
  MPI_File_get_size(myfile, &filesize);
  /* Calculate how many elements that is */
  filesize = filesize/sizeof(double);
  /* Calculate how many elements each processor gets */
  bufsize = filesize/np;
  /* Allocate the buffer to read to, one extra for terminating null char */
  buf = (double *) malloc((bufsize)*sizeof(double));
  /* Set the file view */
  MPI_File_set_view(myfile, myid*bufsize*sizeof(double), MPI_DOUBLE,
             MPI_DOUBLE,"native", MPI_INFO_NULL);
  /* Read from the file */
  MPI_File_read(myfile, buf, bufsize, MPI_DOUBLE, &status);
  /* Find out how many elemyidnts were read */
  MPI_Get_count(&status, MPI_DOUBLE, &nrchar);
  /* Set terminating null char in the string */
  //buf[nrchar] = (double)0;
  printf("Process %2d read %d characters: ", myid, nrchar);

  int j;
  for (j = 0; j <bufsize;j++){
    matrix[myid][j] = buf[j];
  }

  /* Close the file */
  MPI_File_close(&myfile);

  if (myid==0) {
    printf("Done\n");
  }

  MPI_Finalize();
  exit(0);
}

However when I try to call MPI_File_open after I close the first file, I get an error. Do I need multiple communicators to perform this? Any tips will be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:17:43+00:00Added an answer on June 2, 2026 at 9:17 am

    The code in ArrayAllocation above does not quite match the logic of the main program. The matrix is allocated as an array of pointers to vectors of doubles before MPI is initialized, therefore it is impossible to set the number of rows to the number of MPI processes.

    The column_size is also not known until the file size is determined.

    It is a general convention in the C language to store matrices by rows. Violating this convention might confuse you or the reader of your code.

    All in all in order to get this program working you need to declare

     int num_procs, column_size;
    

    as global variables before the definition of ArrayAllocation and move the call to this function down below the line where bufsize is calculated:

     ...
     /* Calculate how many elements each processor gets */
     bufsize = filesize/np;
    
     num_procs = np;
     column_size = bufsize;
     double **matrix = ArrayAllocation();
     ...
    

    With the above modifications this example should work on any MPI implementation that supports MPI-IO. I have tested it with OpenMPI 1.2.8.

    In order to generate a test file you could use for instance the following code:

     FILE* f = fopen(FILENAME,"w");
     double x = 0;
     for(i=0;i<100;i++){
       fwrite(&x, 1,sizeof(double), f);
       x +=0.1;
     }
     fclose(f);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.