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 1107321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:55:15+00:00 2026-05-17T01:55:15+00:00

I am trying to run some tests using OPENmpi processing data in an array

  • 0

I am trying to run some tests using OPENmpi processing data in an array by spliting up the work across nodes (the second part is with matricies). I am running into some problems now because the data array is being initialized every time and I don’t know how to prevent this from happening.

How, using ANSI C can I create a variable length array, using OPENmpi once? I tried making it static and global, but nothing.

#define NUM_THREADS 4
#define NUM_DATA 1000

static int *list = NULL;

int main(int argc, char *argv[]) {
  int numprocs, rank, namelen;
  char processor_name[MPI_MAX_PROCESSOR_NAME];
  int n = NUM_DATA*NUM_DATA;
  printf("hi\n");
  int i;
  if(list == NULL)
  {
     printf("ho\n");
     list = malloc(n*sizeof(int));

    for(i = 0 ; i < n; i++)
    {
      list[i] = rand() % 1000;
    }
  }

  int position;

  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Get_processor_name(processor_name, &namelen);
  printf("Process %d on %s out of %d\n", rank,processor_name, numprocs);

  clock_t start = clock();

  position = n / NUM_THREADS * rank;
  search(list,position, n / NUM_THREADS * (rank + 1));

  printf("Time elapsed: %f seconds\n",  ((double)clock() - (double)start) /(double) CLOCKS_PER_SEC);

  free(list);

  MPI_Finalize();
  return 0;
}
  • 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-05-17T01:55:15+00:00Added an answer on May 17, 2026 at 1:55 am

    Probably the easiest way is to have the rank 0 process do the initialization while the other processes block. Then once the initialization is done, have them all start their work.

    A basic example trying to call your search function (NB: it’s dry-coded):

    #define NUM_THREADS 4
    #define NUM_DATA 1000
    
    int main(int argc, char *argv[]) {
       int *list;
       int numprocs, rank, namelen, i, n;
       int chunksize,offset;
       char processor_name[MPI_MAX_PROCESSOR_NAME];
    
       n= NUM_DATA * NUM_DATA;
    
       MPI_Status stat;
       MPI_Init(&argc, &argv);
       MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
       MPI_Comm_rank(MPI_COMM_WORLD, &rank);
       MPI_Get_processor_name(processor_name, &namelen);
    
       //note you'll need to handle n%NUM_THREADS !=0, but i'm ignoring that for now
       chunksize = n / NUM_THREADS; 
    
       if (rank == 0) {
          //Think of this as a master process
          //Do your initialization in this process
          list = malloc(n*sizeof(int));
    
          for(i = 0 ; i < n; i++)
          {
             list[i] = rand() % 1000;
          }
    
          // Once you're ready, send each slave process a chunk to work on
          offset = chunksize;
          for(i = 1; i < numprocs; i++) {
             MPI_Send(&list[offset], chunksize, MPI_INT, i, 0, MPI_COMM_WORLD);
             offset += chunksize
          }
    
          search(list, 0, chunksize);
    
          //If you need some sort of response back from the slaves, do a recv loop here
       } else {
    
          // If you're not the master, you're a slave process, so wait to receive data
    
          list = malloc(chunksize*sizeof(int));  
          MPI_Recv(list, chunksize, MPI_INT, 0, 0, MPI_COMM_WORLD, &stat);
    
          // Now you can do work on your portion
          search(list, 0, chunksize);
    
          //If you need to send something back to the master, do it here.
       }
    
       MPI_Finalize();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run some unit tests using NUnit while I have the
I'm trying to run some tests in Ant presently using JUnit, and all of
I'm trying to run some unit tests using QUnit written in CoffeeScript but there
I'm trying to run some unit-tests for NxBRE before I start referencing it's implementation
I'm trying to run some statistics over the Stack Overflow data dump, and for
I'm attempting to write some unit tests using EasyMock and TestNG and have run
I am currently trying to run some tests on a RESTful web service and
I am trying to run some acceptance tests for javascript code. However, when I
I'm trying to run some NUnit tests that use NMock2 for mocking. These tests
I'm trying CTest in CMake in order to automatically run some of my tests

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.