int multiplyMatrix(Matrix *ma, Matrix *mb){ // validators if (!validateMatrix(ma) || !validateMatrix(mb)) { printf("[error] invalid matrix\n"); return 1; } if (ma->colNum != mb->rowNum) { printf("[error] matrix A does not match matrix B\n"); return 2; } // multiplication printf("multiply matrix a...\n"); printMatrix(ma); printf("with matrix b\n"); printMatrix(mb); printf("Result:\n"); int ia, i, j, ct, posSum; for (i=0, j=0, ct=0; ct < ma->rowNum * mb->colNum; ++ct) { posSum = 0; i=ct/mb->colNum; j=ct%mb->colNum; for (ia = 0; ia < ma->colNum; ia++) { posSum += (ma->body[i*ma->colNum+ia] * mb->body[ia*mb->colNum+j]); } printf("%4d%c", posSum, j==mb->colNum-1 ? '\n' :' '); fflush(stdout); } return 0; }
int main (int argc, char * argv[]) { int i, j, k; // loop variables double * F, * G; // Create matrix pointers, G is verification matrix //srand(time(NULL)); // Seed randoms // Set size of DIM, default is 4097 from lab requirements if (argc == 1) { DIM = 4097; } else { DIM = atoi(argv[1]); } size_t memSize = DIM * DIM * sizeof(double); // Allocate memory for matrices F = (double *) malloc(memSize); G = (double *) malloc(memSize); if (F == NULL || G == NULL) fprintf(stderr, "Error allocating matrix\n"); // define thread hierarchy int nblocks = NBLOCKS; int tpb = TPB; // initialize with randoms in range [1.0 2.0) for (i = 0; i < DIM; i++) { for (j = 0; j < DIM; j++) { int temp = rand(); temp &= MASK_A; temp |= MASK_B; F[i * DIM + j] = *(float *) &temp; G[i * DIM + j] = *(float *) &temp; } } // Take beginning time time_t time1 = time(NULL); clock_t tick = clock(); // Perform matrix computations computeMatrix(F, DIM); // Take end time and calculate difference time_t time2 = time(NULL); tick = clock() - tick; double timeDiff = difftime(time2, time1); printf("elapsed time\t(clock): %d\n", (int) tick); printf("elapsed time\t (time): %.0f\n", timeDiff); printf("\ndiff: %d, total: %d\n", validateMatrix(F, G), DIM * DIM); printMatrix(F); return(0); }
void printMatrix(Matrix *m) { // validate the matrix if (!validateMatrix(m)) { printf("[error] invalid matrix\n"); return; } int rowNum = m->rowNum, colNum = m->colNum, i, j; for (i = 0; i < rowNum; i++) { for (j = 0; j < colNum; j++) { printf("%4d%c", m->body[i*colNum + j], j==colNum-1?'\n':' '); } } printf("\n"); }
/// Transform a direction vector from local object space to world space const QVector3D directionToWorld( const QVector3D & v ) const { validateMatrix(); return (mModelMatrix * QVector4D(v,0)).toVector3D(); }
/// Returns the transformation matrix to world space const QMatrix4x4 & modelMatrix() const { validateMatrix(); return mModelMatrix; }
/// Transform a point vectorfrom local object space to world space const QVector3D pointToWorld( const QVector3D & v ) const { validateMatrix(); return (mModelMatrix * QVector4D(v,1)).toVector3D(); }
/// Transform a vector from local object space to world space const QVector4D toWorld( const QVector4D & v ) const { validateMatrix(); return mModelMatrix * v; }
/// Returns the vector in world space pointing along the positive local Z axis const QVector3D worldDirection() const { validateMatrix(); return mModelMatrix.column(2).toVector3D(); }
/// Returns the vector in world space pointing along the positive local Y axis const QVector3D worldUp() const { validateMatrix(); return mModelMatrix.column(1).toVector3D(); }
/// The object's position in world space const QVector3D worldPosition() const { validateMatrix(); return mModelMatrix.column(3).toVector3D(); }