Results 1 to 3 of 3

Thread: MPI ptr*

  1. #1
    Join Date
    May 2008
    Location
    Ontario, Canada
    Beans
    Hidden!
    Distro
    Ubuntu

    Question MPI ptr*

    Hey everyone, I am playing around with MPI and parrallel programming.
    It compiles fine, but when I attempt to run the program, it segfaults on me. If anyone is able to see why and point out my mistake I would be gratefull

    My intend is once each process has sorted their parts, they send their data to process 0 (Master) which adds it all up to make the histogram.

    Its quite rough, but I am still learning.
    The code in RED is where problems come up. I am not yet a master of pointers..

    Code:
    #include "mpi.h"
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    const int SEED = 132941;
    
    /* Function Prototype */
    //void histogram(int N, double m, double M, int nbins, int *bin_counts, double *bins);
    
    void histogram(int N, double m, double M, int nbins, int *bin_counts) {
    
        int comm_sz;    /* Number of Processes. */
        int my_rank;    /* My process rank. */
    
        int dest;
        int offset;
        int i;
        int j;
        int tag1;
        int tag2;
    
        double bin_width;                   /* Sets Bin width. */
        double bin_maxes[nbins];            /* Hold max value of each bin. */
    
    
        /* Calculate the min-max value of each bin */
        bin_width = (M-m)/nbins;
        for(j=0; j<nbins; j++)
            bin_maxes[j] = m + bin_width*(j+1);
    
    
        MPI_Status status;
    
        /***** Initializations *****/
        MPI_Init(NULL,NULL);
        MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
        MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    
        printf ("MPI task %d has started...\n", my_rank);
    
        /*chunk sizes*/
        int chunksize[comm_sz-1];
        int r, r_alocated = 0;
        for(r=0; r<(comm_sz-1); r++) {
            chunksize[r] = (int)ceil((N-r_alocated)/(comm_sz-1-r));
            r_alocated += chunksize[r];
        }
    
        /* Declaring the array with all the data.
           Every single processor has this array.
           Waste of ram...but don't know how to fix as of yet.
         */
        double *data = (double *)malloc(N* sizeof(double));
        if (data == NULL) {
            fprintf (stderr, "Insufficient memory Available!" );
            exit(1);
        }
    
        tag2 = 1;
        tag1 = 2;
    
        /***** Master task ******/
        if (my_rank == 0){
    
            /* Fill the array */
    
                srand(SEED);
                int i;
                for (i=0; i<N; i++){
                    //drand==0, then m              is smallest number
                    //drand==1, then m + 1*(M-m)    is largest number
                    data[i] = m + drand48()*(M-m);
                }
    
            /* Send each task its portion of the array */
            offset = 0;
            for (dest=1; dest<comm_sz; dest++) {
                MPI_Send(&offset, 1, MPI_INT, dest, tag1, MPI_COMM_WORLD);
                MPI_Send(&data[offset], chunksize[dest-1], MPI_FLOAT, dest, tag2, MPI_COMM_WORLD);
                printf("Sent %d elements to task %d offset= %d\n",chunksize[dest-1],dest,offset);
                offset = offset + chunksize[dest-1];
            }
    
        /* Other Processes */
        } else {
            offset = 0;
            for(dest=1; dest<comm_sz;dest++) {
                if(dest==my_rank) {
                    MPI_Recv(&offset, 1, MPI_INT, 0, tag1, MPI_COMM_WORLD, &status);
                    MPI_Recv(&data[offset], chunksize[dest-1], MPI_DOUBLE_PRECISION, 0, tag2, MPI_COMM_WORLD, &status);
                    printf("Sup from %d::Received %d amount of data with offset %d!\n",my_rank,chunksize[dest-1],offset);
    
                    //Now that the process has the data, sort it into bins
    
                    for(i=offset; i < offset + chunksize[dest-1]; i++) {
                        //Check in which bin data[i] belongs, using bin_maxes
                        int ii;
                        for(ii=0; ii<nbins;ii++){
                            if(data[i]>(bin_maxes[i]-bin_width) && data[i]<bin_maxes[i]){
                                bin_counts[i]++;
                                 //This here is giving me the problem 
    
                            }
                        }
                    }
                }
                offset = offset + chunksize[dest-1];
            }
        }
    
        MPI_Finalize();
    }
    
    int main(int argc, char *argv[]){
    
        int N = 1000;
        double m = 5.23;
        double M = 27.64;
        int nbins = 10;
    //    double *bins = (double *)malloc((nbins+1) * sizeof(double));
        int *bin_counts = (int *)malloc(nbins * sizeof(int));
    
        int k;
        for(k=0; k<nbins; k++)
            bin_counts[k]=0;
    
        histogram(N,m,M,nbins, bin_counts);
    
        return 0;
    }

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: MPI ptr*

    my guess is that bin_counts[i] should be bin_counts[ii].
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    May 2008
    Location
    Ontario, Canada
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: MPI ptr*

    Bachstelze: Thank you. You are correct. It was going out of bounds of the array!

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •