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

  • Home
  • SEARCH
  • 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 8480945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:25:57+00:00 2026-06-10T19:25:57+00:00

How do I access a global vector from an individual thread in MPI? I’m

  • 0

How do I access a global vector from an individual thread in MPI?

I’m using a library – specifically, an ODE solver library – called CVODE (part of SUNDIALS). The library works with MPI, so that multiple threads are running in parallel. They are all running the same code. Each thread sends the thread “next to” it a piece of data. But I want one of the threads (rank=0) to print out the state of the data at some points.

The library includes functions so that each thread can access their own data (the local vector). But there is no method to access the global vector.

I need to output the values of all of the equations at specific times. To do so, I would need access to the global vector. Anyone know how get at all of the data in an MPI vector (using CVODE, if possible)?

For example, here is my code that each thread runs

  for (iout=1, tout=T1; iout <= NOUT; iout++, tout += DTOUT) {
    flag = CVode(cvode_mem, tout, u, &t, CV_NORMAL);
    if(check_flag(&flag, "CVode", 1, my_pe)) break;
    if (my_pe == 0) PrintData(t, u);
  }
...
static void PrintData(realtype t, N_Vector u) {
   I want to print data from all threads in here
}

In function f (the function I’m solving), I pass data back and forth using MPI_Send and MPI_Recv. But I can’t really do that in PrintData because the other processes have run ahead. Also, I don’t want to add messaging overhead. I want to access the global vector in PrintData, and then just print out what’s needed. Is it possible?

Edit: While waiting for a better answer, I programmed each thread passing the data back to the 0th thread. I don’t think that’s adding too much messaging overhead, but I’d still like to hear from you experts if there’s a better method (I’m sure there isn’t any worse ones! 😀 ).

Edit 2: Although angainor’s solution is surely superior, I stuck with the one I had created. For future reference of anyone who has the same question, here is the basics of how I did it:

/* Is called by all threads */
static void PrintData(realtype t, N_Vector u, UserData data) {

... declarations and such ...

  for (n=1; n<=my_length; n++) {
    mass_num = my_base + n;
    z[mass_num - 1] = udata[n-1];
    z[mass_num - 1 + N] = udata[n - 1 + my_length];
  }

  if (my_pe != 0) {
    MPI_Send(&z, 2*N, PVEC_REAL_MPI_TYPE, 0, my_pe, comm);

  } else {

    for (i=1; i<npes; i++) {
      MPI_Recv(&z1, 2*N, PVEC_REAL_MPI_TYPE, i, i, comm, &status);
      for (n=0; n<2*N; n++)
        z[n] = z[n] + z1[n];
    }

... now I can print it out however I like...

  return;
}
  • 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-10T19:25:59+00:00Added an answer on June 10, 2026 at 7:25 pm

    When using MPI the individual threads do not have access to a ‘global’
    vector. They are not threads, they are processes that can run on
    different physical computers and therefore can not have direct access to global data.

    To do what you want you can either send the vector to one of the MPI processes (you did that) and print it there, or to print local worker parts in sequence. Use a function like this:

    void MPI_write_ivector(int thrid, int nthr, int vec_dim, int *v)
    {
      int i, j;
      int curthr = 0;
    
      MPI_Barrier(MPI_COMM_WORLD);
      while(curthr!=nthr){
        if(curthr==thrid){
          printf("thread %i writing\n", thrid);
          for(i=0; i<vec_dim; i++) printf("%d\n", v[i]);
          fflush(stdout);
          curthr++;
          MPI_Bcast(&curthr, 1, MPI_INT, thrid, MPI_COMM_WORLD);
        } else {
          MPI_Bcast(&curthr, 1, MPI_INT, curthr, MPI_COMM_WORLD);
        }
      }
    }
    

    All MPI processes should call it at the same time since there is a barrier and broadcast inside. Essentially, the procedure makes sure that all the MPI processes print their vector part in order, starting from rank 0. The data is not messed up since only
    one process writes at any given time.

    In the example above, Broadcast is used since it gives more flexibility on the order in which the threads should print their results – the thread that currently outputs can decide, who comes next. You could also skip the broadcast and only use a barrier

    void MPI_write_ivector(int thrid, int nthr, int vec_dim, int *v)
    {
      int i, j;
      int curthr = 0;
    
      while(curthr!=nthr){
        if(curthr==thrid){
          printf("thread %i writing\n", thrid);
          for(i=0; i<vec_dim; i++) printf("%d\n", v[i]);
          fflush(stdout);
        }
        MPI_Barrier(MPI_COMM_WORLD);
        curthr++;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Should I access global db object directly from within the methods of each class?
I was wondering how to access a global function fn in ruby from a
I'm curious as to the difference of denying access using the global $_server['http_referer'] and
So I'm finding that I'm using a singleton User class to access global variables
For some reason I have to access Jenkins global environment variables like BUILD ID,
I have a singleton class for global access to config information. This singleton class
We have a need to access kernel global vars in net/ipv4/af_inet.c that are not
I want to get access to the data-global-id value in the following markup and
I want to access the user data as global in whole website. I make
Access denied for user 'root '@'localhost' (using password: YES) Yes, this error is all

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.