4

Given:

1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8

I want to split the 2d array (struct MATRIX) into the an array of struct MATRIX given a chunksize CS: assume cs to be 2, the answer would be

Seg[0]:
1 2 
1 2 
1 2
Seg[1]:
3 4 
3 4 
3 4
....
Seg[3]:
7 8
7 8
7 8

Here is my Matrix Struct:

typedef struct MATRIX {
    int nrow;
    int ncol;
    int **element;
} MATRIX;

and here is the function the seperates them:

void SegmentMatrix(MATRIX input,MATRIX* segs,int Chunksize, int p) {
    int i,j,r;

    //Allocate segs
    for (i = 0; i<p;i++)
    {
        CreateMatrix(&(segs[i]),input.nrow ,Chunksize,0);
    }

    //Now Copy the elements from input to the segs
    //where seg0 takes from 0 to cs cols of a, and all their rows, and seg1 takes from cs to 2cs ...
    printf("Stats:\n\t P: %d\t CS: %d\n",p,Chunksize);
    for (r = 0; r<p; r++) {
        for (i = 0; i<input.nrow;i++) {
            for (j = r*Chunksize; j<r*Chunksize+Chunksize-1; j++) {
                 //I tried (&(segs[r]))->element... Doesn't work, produces wrong data
                 segs[r].element[i][j] = input.element[i][j];

        }
    }
    PRINTM(segs[r]);
    }


}

Note that PRINTM basically prints the matrix, it knows the limits by checking segs[r].nrow and ncol and CreateMatrix takes the following inputs (&matrix, number of rows, number of colums, filltype) and mallocs from within.

filltype: 
0- generates zeroth matrix
1- generates identity
else A[i][j] = j; for simplicity

The problem is that the if i print the matrices Segs[i], they all come down with their default value given by CreateMatrix, and not the newly added values.

CLARIFICATION: Okay, so if you guys check that last PRINTM in SegmentMatrix function, it outputs the matrices as if the for loops didn't happen, aka, i can delete the for loops and would get the same output.. did i do something wrong in this line (taken from the SegmentMatrix)

Segs[r].element[i][j] = input.element[i][j];
3
  • Where would you place your call to PRINTM to have the erroneous input appearing? I would like to see that called out in your above code to make sure it is not a cart before the horse type problem. Commented May 13, 2013 at 19:03
  • If you look at the last statement in SegmentMatrix you will see the PRINTM, it shows the default values of the segs, as if the whole for loops didn't happen Commented May 13, 2013 at 21:11
  • @MichaelDorgan hope that explains it Commented May 13, 2013 at 21:22

2 Answers 2

6

I don't see why and what you are manipulating with multiplication by ChunkSize and r (which is uninitialized anyway), I'd suggest simplifying the code (rule of thumb: if it seems messy, it's too complex). All you need is a 3-dimensional array to store the array of chunks, and modulo arithmetic plus integer division to insert into the appropriate column of the appropriate chunk:

/* the variable-sized dimension of the `chunks' argument is w / chsz elements big
 * (it's the number of chunks)
 */
void split(int h, int w, int mat[h][w], int chsz, int chunks[][h][chsz])
{
    /* go through each row */
    for (int i = 0; i < h; i++) {
        /* and in each row, go through each column */
        for (int j = 0; j < w; j++) {
            /* and for each column, find which chunk it goes in
             * (that's j / chsz), and put it into the proper row
             * (which is j % chsz)
             */
            chunks[j / chsz][i][j % chsz] = mat[i][j];
        }
    }
}

Demonstration, a. k. a. how to call it:

int main(int agrc, char *argv[])
{
    const size_t w = 8;
    const size_t h = 3;
    const size_t c = 2;

    int mat[h][w] = {
        { 1, 2, 3, 4, 5, 6, 7, 8 },
        { 1, 2, 3, 4, 5, 6, 7, 8 },
        { 1, 2, 3, 4, 5, 6, 7, 8 }
    };

    int chunks[w / c][h][c];

    split(h, w, mat, c, chunks);

    for (int i = 0; i < w / c; i++) {
        for (int j = 0; j < h; j++) {
            for (int k = 0; k < c; k++) {
                printf("%3d ", chunks[i][j][k]);
            }
            printf("\n");
        }
        printf("\n\n");
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

+1 think he should accept your answer . i was too tired to check his code . so simple wrote my pseudo code
Thank you guys... i will look at it and respond ASAP, but the structs are there to keep count of the size of the matrix just in case
This is part of a program that is supposed to read matrices from files, and operates on them using MPI (MPICH2) This part (the one i have posted) is about multiplying them. I have done the serial one easily, using the struct i have above. Chunksize is literally the number of columns i would send from matrix A to the nodes, it's calculated in process 0 and it's just passed to the function (i don't see why u think it's uninit) and P is, the number of processes. The last for loop is to make sure that no matter how many processes i've got i can summon this loop correctly, @H2CO3
2

Question was unclear . so i thought he wanted just to know how to achieve this. So i wrote this simple Pseudo code . Otherwise accept my apologize :

matrix[i] matrix
//matrixes total column size should be bigger big 2d array column size
first condition check: sum(matrix[i].colsize)>=big2d.colsize
//in this simple code raw sizes must be equal
second condition: for all i matrix[i].rawsize=big2d.rawsize
//if columns sizes will be equal the algorithm could be simplified , does not mean optimized
 //splitting big2d into matrixes
for (int br=0;br<big2d.rawsize;br++){
i=0;//store matrix index
int previndex=0;//store offset for next matrix
  for(int bc=0;bc<big2d.colsize;bc++){

      matrix[i].val[bc-previndex][br]=big2d.val[bc][br]; //assign (bc,br) 

      if(bc-previndex==matrix[i].colsize-1){
             i++; //move to next matrix;//if we not have next matrix then break;
            previndex=bc+1; 
          }
     /*if it be for equal chunks matrixes offset can be calculated this way too
         matrix[bc/chunk].val[bc%chunk][br]=big2d.val[bc][br];
      */
  }//loop columns
}//loop raws

1 Comment

Please, add some comments on exactly what your code is doing. It is not clear by itself...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.