Q1: // creates a new matrix from a group of consecutive rows of an old one #include #include #include #include int onedim(int row, int col, int nCols) { return row * nCols + col; } // copies nRowsSubM consecutive rows of m to subMat, starting with // row firstRow in m; m is nRowsM by nColsM int *getRows(int *m, int nRowsM, int nColsM, int firstRow, int nRowsSubM) { int *subMat = malloc(nRowsSubM * nColsM * sizeof(int)); #pragma omp parallel { int *startFrom,*startTo,i; #pragma omp for for (int i = 0; i < nRowsSubM; i++) { startFrom = m + onedim(firstRow+i,0,nColsM); startTo = subMat + onedim(i,0,nColsM); memcpy(startTo, startFrom, nColsM * sizeof(int)); } } return subMat; } int main() { // intended as a 4x3 matrix int z[12] = {5,12,13,6,2,22,15,3,1,2,3,4}; omp_set_num_threads(2); // extract the last 2 cols int *outM = getRows(z,4,3,1,2); // should print // 6 2 22 // 15 3 1 for (int i = 0; i < 2; i++) { printf("%d %d %d\n",outM[3*i],outM[3*i+1],outM[3*i+2]); } }