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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:17:12+00:00 2026-06-14T12:17:12+00:00

I don’t get why MPI_Reduce() does a segmentation fault as soon as I use

  • 0

I don’t get why MPI_Reduce() does a segmentation fault as soon as I use a custom MPI datatype which contains dynamically allocated arrays. Does anyone know ? The following code crashes with 2 processors, inside the MPI_Reduce().
However If I remove the member double *d int MyType and changes the operator and MPI type routines accordingly, the reduction is done without any problem.

Is there a problem using dynamically allocated arrays or is there something fundamentally wrong with what I do :

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



typedef struct mytype_s
{
    int c[2];
    double a;
    double b;
    double *d;
} MyType;



void CreateMyTypeMPI(MyType *mt, MPI_Datatype *MyTypeMPI)
{
    int block_lengths[4];                        // # of elt. in each block
    MPI_Aint displacements[4];                   // displac.
    MPI_Datatype typelist[4];                    // list of types
    MPI_Aint start_address, address;            // use for calculating displac.
    MPI_Datatype myType;

    block_lengths[0] = 2;
    block_lengths[1] = 1;
    block_lengths[2] = 1;
    block_lengths[3] = 10;

    typelist[0] = MPI_INT;
    typelist[1] = MPI_DOUBLE;
    typelist[2] = MPI_DOUBLE;
    typelist[3] = MPI_DOUBLE;

    displacements[0] = 0;

    MPI_Address(&mt->c, &start_address);
    MPI_Address(&mt->a, &address);
    displacements[1] = address - start_address;

    MPI_Address(&mt->b,&address);
    displacements[2] = address-start_address;

    MPI_Address(&mt->d, &address);
    displacements[3] = address-start_address;

    MPI_Type_struct(4,block_lengths, displacements,typelist,MyTypeMPI);
    MPI_Type_commit(MyTypeMPI);
}




void MyTypeOp(MyType *in, MyType *out, int *len, MPI_Datatype *typeptr)
{
    int i;
    int j;

    for (i=0; i < *len; i++)
    {
        out[i].a += in[i].a;
        out[i].b += in[i].b;
        out[i].c[0] += in[i].c[0];
        out[i].c[1] += in[i].c[1];

        for (j=0; j<10; j++)
        {
            out[i].d[j] += in[i].d[j];
        }
    }
}




int main(int argc, char **argv)
{
    MyType mt;
    MyType mt2;
    MPI_Datatype MyTypeMPI;
    MPI_Op MyOp;
    int rank;
    int i;

    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);


    mt.a = 2;
    mt.b = 4;
    mt.c[0] = 6;
    mt.c[1] = 8;
    mt.d = calloc(10,sizeof *mt.d);
    for (i=0; i<10; i++) mt.d[i] = 2.1;

    mt2.a = 0;
    mt2.b = 0;
    mt2.c[0] = mt2.c[1] = 0;
    mt2.d = calloc(10,sizeof *mt2.d);


    CreateMyTypeMPI(&mt, &MyTypeMPI);
    MPI_Op_create((MPI_User_function *) MyTypeOp,1,&MyOp);

    if(rank==0) printf("type and operator are created now\n");

    MPI_Reduce(&mt,&mt2,1,MyTypeMPI,MyOp,0,MPI_COMM_WORLD);

    if(rank==0)
    {




        for (i=0; i<10; i++) printf("%f ",mt2.d[i]);
        printf("\n");
    }

    free(mt.d);
    free(mt2.d);
    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-06-14T12:17:13+00:00Added an answer on June 14, 2026 at 12:17 pm

    Let’s look at your struct:

    typedef struct mytype_s
    {
        int c[2];
        double a;
        double b;
        double *d;
    } MyType;
    
    ...
    
    MyType mt;
    mt.d = calloc(10,sizeof *mt.d);
    

    And your description of this struct as an MPI type:

    displacements[0] = 0;
    
    MPI_Address(&mt->c, &start_address);
    MPI_Address(&mt->a, &address);
    displacements[1] = address - start_address;
    
    MPI_Address(&mt->b,&address);
    displacements[2] = address-start_address;
    
    MPI_Address(&mt->d, &address);
    displacements[3] = address-start_address;
    
    MPI_Type_struct(4,block_lengths, displacements,typelist,MyTypeMPI);
    

    The problem is, this MPI struct is only ever going to apply to the one instance of the structure you’ve used in the definition here. You have no control at all of where calloc() decides to grab memory from; it could be anywhere in virtual memory. The next one of these type you create and instantiate, the displacement for your d array will be completely different; and even using the same struct, if you change the size of the array with realloc() of the current mt, it could end up having a different displacement.

    So when you send, receive, reduce, or anything else with one of these types, the MPI library will dutifully go to a probably meaningless displacement, and try to read or write from there, and that’ll likely cause a segfault.

    Note that this isn’t an MPI thing; in using any low-level communications library, or for that matter trying to write out/read in from disk, you’d have the same problem.

    Your options include manually “marshalling” the array into a message, either with the other fields or without; or adding some predictability to where d is located such as by defining it to be an array of some defined maximum size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know what is causing this but I try to get the level which
Don't these two mean the same thing, first get the value and then increment?
Don't be afraid to use any technical jargon or low-level explanations for things, please.
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't know a whole lot about streams. Why does the first version work using
Don't know how to explain it better but i'm trying to get a response
Don't ask me how but I managed to get accidentally the following remote branches
Don't get me wrong PDO is great but what I don't like about it,
Don't know if this has been answered before. Have custom routes to users. If
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.