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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:18:17+00:00 2026-05-19T12:18:17+00:00

I am trying to write a program in c using the MPI library. In

  • 0

I am trying to write a program in c using the MPI library.
In my program I am solving TSP (but not with any special algorithm…).
My input parameters are int nCites, int xCoord[] and int yCoord[].
I am bundling them into Coordinates and using MPI_Bcast to make them available to all the threads.

My problem is this: after I’ve finished calculating the weight of all the paths in each thread, I want to reduce them into one single result, the best one. I’ve tried using MPI_Reduce, but something, and this is where I get confused, causes a segmentation fault (only in one of the threads, usually root).

This is the main code and structs:

typedef struct Coordinates_t {
    int* x;
    int* y;
    int n;
} Coordinates;

typedef struct PathAndLength_t {
    int* path;
    int pathSize;
    int length;
} PathAndLength;

void comparePaths(void* a, void* b, int* len, MPI_Datatype* dataType) {
    ...
}

int tsp_main(int nCites, int xCoord[], int yCoord[], int P[]){
    int numOfProcs, rank;
    if (MPI_Comm_size(MPI_COMM_WORLD, &numOfProcs))
        throw "Error: MPI_Comm_size failed";
    if (MPI_Comm_rank(MPI_COMM_WORLD, &rank))
        throw "Error: MPI_Comm_rank failed";

    Coordinates crds;
    crds.x = xCoord;
    crds.y = yCoord;
    crds.n = nCites;

    MPI_Datatype data;
    createInDataType(&crds, &data);

    if (MPI_Bcast(&crds, 1, data, 0, MPI_COMM_WORLD))
        throw "Error: MPI_Comm_size failed";

        ...

    PathAndLength* pal = (PathAndLength*)malloc(sizeof(PathAndLength));
    pal->path = (int*)malloc(sizeof(int)*crds.n);

    pal->length = min_length;
    for (int i = 0; i < crds.n; ++i) {
        (pal->path)[i] = min_path[i];
    }
    pal->pathSize = crds.n;
    MPI_Datatype outDatatype;
    MPI_Op op;

    createOutDataType(pal, &outDatatype);

    MPI_Op_create(&comparePaths, 1, &op);

    PathAndLength* result = (PathAndLength*)malloc(sizeof(PathAndLength));
    result->path = (int*)malloc(sizeof(int)*crds.n);
    MPI_Reduce(pal, result, crds.n, outDatatype, op, 0, MPI_COMM_WORLD);

    ...

    return result->length;
}

And these are the createOutDataType and createInDataType I use in my code:

void createInDataType(Coordinates* indata, MPI_Datatype* message_type_ptr) {
    // Build a derived datatype
    int block_lengths[3];
    MPI_Aint displacements[3];
    MPI_Aint addresses[4];
    MPI_Datatype typelist[3];

    // First specify the types
    typelist[0] = typelist[1] = typelist[2] = MPI_INT;

    // Specify the number of elements of each type
    block_lengths[0] = block_lengths[1] = indata->n;
    block_lengths[2] = 1;

    // Calculate the displacements of the members relative to indata
    MPI_Address(indata, &addresses[0]);
    MPI_Address(indata->x, &addresses[1]);
    MPI_Address(indata->y, &addresses[2]);
    MPI_Address(&indata->n, &addresses[3]);
    displacements[0] = addresses[1] - addresses[0];
    displacements[1] = addresses[2] - addresses[0];
    displacements[2] = addresses[3] - addresses[0];

    // Create the derived type
    MPI_Type_struct(3, block_lengths, displacements, typelist, message_type_ptr);

    // Commit it so that it can be used
    MPI_Type_commit(message_type_ptr);
}

void createOutDataType(PathAndLength* outdata, MPI_Datatype* message_type_ptr) {
    // Build a derived datatype
    int block_lengths[2];
    MPI_Aint displacements[2];
    MPI_Aint addresses[3];
    MPI_Datatype typelist[2];

    // First specify the types
    typelist[0] = MPI_INT;
    typelist[1] = MPI_INT;

    // Specify the number of elements of each type
    block_lengths[0] = outdata->pathSize;
    block_lengths[1] = 1;

    // Calculate the displacements of the members relative to outdata
    MPI_Address(outdata, &addresses[0]);
    MPI_Address(outdata->path, &addresses[1]);
    MPI_Address(&outdata->length, &addresses[2]);
    displacements[0] = addresses[1] - addresses[0];
    displacements[1] = addresses[2] - addresses[0];

    // Create the derived type
    MPI_Type_struct(2, block_lengths, displacements, typelist, message_type_ptr);

    // Commit it so that it can be used
    MPI_Type_commit(message_type_ptr);
}

Sorry for including so much code, but I couldn’t decide what, if any, was irrelevant…
Thank you.

  • 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-19T12:18:18+00:00Added an answer on May 19, 2026 at 12:18 pm
    PathAndLength* result = (PathAndLength*)malloc(sizeof(PathAndLength));
    result->path = (int*)malloc(sizeof(int)*crds.n);
    MPI_Reduce(pal, result, crds.n, outDatatype, op, 0, MPI_COMM_WORLD);
    

    you are receiving crds.n*outDatatypes into result buffer of size sizeof(PathAndLength). You seem to have design flaw.

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

Sidebar

Related Questions

I'm trying to write a small baseball statistics program using data from retrosheet.org, but
I use VS2k8 to write and compile (but not run) a program using the
I am trying to write a program using python-fuse, but I can't get file
now, i'm trying to write program using php and sqlite, but i have problem.
I'm trying to write a rather trivial program using open gl on linux, but
I'm trying to write a program using MPI and I have a question that
I'm trying to write a matrix-vector multiplication program using MPI. I'm trying to send
I'm trying to write a program that can stop and start services using SilverLight
i am trying to write a program in java (on linux) using RandomAccessFile class
Hi I am trying to write a program that separates words into syllables using

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.