/* Executes program functions. */ int main() { setvbuf(stdout, NULL, _IONBF, 0); int row, col, r1, r2; int matrix1[ROW_SIZE][COL_SIZE]; int matrix2[ROW_SIZE][COL_SIZE]; int mat_trans[ROW_SIZE][COL_SIZE]; for (row = 0; row < ROW_SIZE; row++) { for (col = 0; col < COL_SIZE; col++) { r1 = rand() % 10; r2 = rand() % 10; matrix1[row][col] = r1; matrix2[row][col] = r2; } } printf("\nMatrix1:\n"); displayResults(matrix1); printf("\nMatrix2:\n"); displayResults(matrix2); memcpy(mat_trans, transpose(matrix2), sizeof(int) * ROW_SIZE * COL_SIZE); /* WARNING: passing argument 2 of 'memcpy' makes pointer from integer without a cast [enabled by default] */ multiplyMatrices(matrix1, mat_trans); multiplyMatrices(matrix1, matrix2); return 0; }
// Computes the probabilities given the set of images void computeProb(void* sdram_ptr, double** wL1, double** wL2, double** wL3, double** bias, double*** probabilities) { int i, j, k; // i represents rows, j represents columns double** layer1 = create2DDoubleArray(LAYER1, NUMTEST); printf("Computing layer 1...\n"); for(i = 0; i < LAYER1; i++) { for(j = 0; j < NUMTEST; j++) { layer1[i][j] = 0; for(k = 0; k < IMGSIZE; k++) { layer1[i][j] += wL1[i][k] * ((double*)sdram_ptr)[k * NUMIMGS + j]; } } printf("Computing Layer 1 Node #: %d\n", i); } printf("Applying layer 1 bias and sigmoid...\n"); for(i = 0; i < LAYER1; i++) { for(j = 0; j < NUMTEST; j++) { layer1[i][j] += bias[i][0]; layer1[i][j] = 1 / (1 + exp(1 - layer1[i][j])); } } printf("Layer 1 computation complete!\n"); printf("Computing layer 2...\n"); double** layer2 = multiplyMatrices(wL2, LAYER2, LAYER1, layer1, LAYER1, NUMTEST); printf("Applying layer 2 bias and sigmoid...\n"); for(i = 0; i < LAYER2; i++) { for(j = 0; j < NUMTEST; j++) { layer2[i][j] += bias[i][1]; layer2[i][j] = 1 / (1 + exp(1 - layer2[i][j])); } } printf("Layer 2 computation complete!\n"); free(layer1); printf("Computing final probabilities...\n"); (*probabilities) = multiplyMatrices(wL3, LAYERFINAL, LAYER2, layer2, LAYER2, NUMTEST); printf("Applying probability sigmoid...\n"); for(i = 0; i < LAYERFINAL; i++) { for(j = 0; j < NUMTEST; j++) { (*probabilities)[i][j] = 1 / (1 + exp(1 - (*probabilities)[i][j])); } } printf("Probability computation complete!\n"); free(layer2); // array2DtoCSV(probabilities, LAYERFINAL, NUMTEST); }
void matrixSquareRoot(Matrix* A, Matrix* S) { //Approach is to do an SVD of A, then set S to V * sqrt(Sigma) * Vtransposed int i, j; Matrix* U = allocateMatrix(A->rows, A->columns); Matrix* Vtransposed = allocateMatrix(A->rows, A->columns); Matrix* Sigma = allocateMatrix(A->rows, A->columns); //First do an SVD singularValueDecomposition(A, U, Vtransposed, Sigma); //Now calculate sqrt(Sigma) * Vtransposed and stick it in U for(i = 0; i < Sigma->rows; i++) { for(j = 0; j < Sigma->columns; j++) { U->pointer[i + j * U->rows] = Vtransposed->pointer[i + j * Vtransposed->rows] * sqrt(Sigma->pointer[i + i * Sigma->rows]); } } multiplyMatrices(Vtransposed, U, S, 1, 0); freeMatrix(U); freeMatrix(Vtransposed); freeMatrix(Sigma); }
static void fpsCameraViewMatrix(GLFWwindow *window, float *view) { // initial camera config static float position[] = { 0.0f, 0.3f, 1.5f }; static float rotation[] = { 0.0f, 0.0f }; // mouse look static double lastMouse[] = { 0.0, 0.0 }; double mouse[2]; glfwGetCursorPos(window, &mouse[0], &mouse[1]); if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) { rotation[0] += (float)(mouse[1] - lastMouse[1]) * -0.2f; rotation[1] += (float)(mouse[0] - lastMouse[0]) * -0.2f; } lastMouse[0] = mouse[0]; lastMouse[1] = mouse[1]; float rotationY[16], rotationX[16], rotationYX[16]; rotationMatrix(rotationX, rotation[0], 1.0f, 0.0f, 0.0f); rotationMatrix(rotationY, rotation[1], 0.0f, 1.0f, 0.0f); multiplyMatrices(rotationYX, rotationY, rotationX); // keyboard movement (WSADEQ) float speed = (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) ? 0.1f : 0.01f; float movement[3] = {0}; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) movement[2] -= speed; if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) movement[2] += speed; if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) movement[0] -= speed; if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) movement[0] += speed; if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) movement[1] -= speed; if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) movement[1] += speed; float worldMovement[3]; transformPosition(worldMovement, rotationYX, movement); position[0] += worldMovement[0]; position[1] += worldMovement[1]; position[2] += worldMovement[2]; // construct view matrix float inverseRotation[16], inverseTranslation[16]; transposeMatrix(inverseRotation, rotationYX); translationMatrix(inverseTranslation, -position[0], -position[1], -position[2]); multiplyMatrices(view, inverseRotation, inverseTranslation); // = inverse(translation(position) * rotationYX); }
void fastExponentiate(ull x[2][2],ull N,ull mod){ ull retVal[2][2] = { {1,0}, {0,1} }; while(N>0){ if(N&1){ // if odd multiplyMatrices(retVal,x,mod); N--; } multiplyMatrices(x,x,mod); N = N>>1; // divide by two } x[0][0]=retVal[0][0]; x[1][0]=retVal[1][0]; x[0][1]=retVal[0][1]; x[1][1]=retVal[1][1]; }
void wezside::GLUtils::translateMatrix(Matrix* m, float x, float y, float z) { Matrix translation = IDENTITY_MATRIX; translation.m[12] = x; translation.m[13] = y; translation.m[14] = z; memcpy(m->m, multiplyMatrices(m, &translation).m, sizeof(m->m)); }
void wezside::GLUtils::scaleMatrix(Matrix* m, float x, float y, float z) { Matrix scale = IDENTITY_MATRIX; scale.m[0] = x; scale.m[5] = y; scale.m[10] = z; memcpy(m->m, multiplyMatrices(m, &scale).m, sizeof(m->m)); }
// Q = (I - A)(I + A)^{-1} void getCayleyTransform(const double *source, int dim, double *target) { double temp1[dim * dim]; double temp2[dim * dim]; double temp3[dim * dim]; double *plusMatrix = temp1; // temp1 in use; I+A double *minusMatrix = temp2; // temp2 in use; I-A double *identity = target; // I // copy in I+A to lower triangle for inverse, and set scratch to I, // copy I-A to temp int offset = 0; for (int col = 0; col < dim; ++col) { offset = col * (dim + 1); identity[offset] = 1.0; plusMatrix[offset] = 1.0; minusMatrix[offset] = 1.0; ++offset; for (int row = col + 1; row < dim; ++row) { identity[offset] = identity[col + row * dim] = 0.0; plusMatrix[offset] = source[offset]; plusMatrix[col + row * dim] = source[col + row * dim]; minusMatrix[offset] = -source[offset]; minusMatrix[col + row * dim] = -source[col + row * dim]; ++offset; } } double *inverseMatrix = temp3; // temp3 in use; (I+Q)^{-1} int lapackResult = solveSystem(plusMatrix, dim, identity, dim, inverseMatrix); // temp1 free if (lapackResult != 0) { if (lapackResult < 0) { error("error in call to LAPACK routine 'dgesvx': argument %d illegal", -lapackResult); } else if (lapackResult <= dim ){ error("error in call to LAPACK routine 'dgesvx': factor U(%d) is exactly 0 so that U is singular", lapackResult); } else { error("error in call to LAPACK routine 'dgesvx': reciprocal condition estimate below machine tolerance", lapackResult); } } multiplyMatrices(minusMatrix, dim, dim, // temp2, temp3 free inverseMatrix, dim, dim, target); }
void compProb(double*** probabilities, double** layer2, double** wL3) { (*probabilities) = multiplyMatrices(wL3, LAYERFINAL, LAYER2, layer2, LAYER2, NUMTEST); int i, j; // i represents rows, j represents columns for(i = 0; i < LAYERFINAL; i++) { for(j = 0; j < NUMTEST; j++) { (*probabilities)[i][j] = 1 / (1 + exp(1 - (*probabilities)[i][j])); } } }
void wezside::GLUtils::rotateAboutZ(Matrix* m, float angle) { Matrix rotation = IDENTITY_MATRIX; float sine = (float)sin(angle); float cosine = (float)cos(angle); rotation.m[0] = cosine; rotation.m[1] = -sine; rotation.m[4] = sine; rotation.m[5] = cosine; memcpy(m->m, multiplyMatrices(m, &rotation).m, sizeof(m->m)); }
static int multiplyMatrices_test() { double correctAnswer[] = { 0.35457178346387, 0.0343816817133231, 0.20314407828111, 0.312932333295974, 0.74624402508098, 0.218216102874039, 0.55447046989404, 0.784423793782448, 1.23265220887754, 0.22110356807718, 0.79461564441162, 1.17551576373053 }; double result[testMatrix1Rows * testMatrix2Columns]; multiplyMatrices((double *) testMatrix1, testMatrix1Rows, testMatrix1Columns, (double *) testMatrix2, testMatrix2Rows, testMatrix2Columns, result); return (allApproximatelyEqual(result, correctAnswer, testMatrix1Rows * testMatrix2Columns, TEST_TOLERANCE)); }
void rotateCoordinates(ligand *lig, ligand *ligNew, box *pbc, float stepSize, float *minR, float *maxR) { /* printf("CA_CA -- in rotateCoordinates: minR: %f %f %f maxR: %f %f %f \n", minR[0], minR[1], minR[2], maxR[0], maxR[1], maxR[2]); int t; for (t=0; t<lig->nAtoms; t++) { printf("CA_CA at start of rotateCoordinates i %d lig->atom[i].r %f %f %f\n", t, lig->atom[t].r[0], lig->atom[t].r[1], lig->atom[t].r[2]); } printf("CA_CA \n"); */ float phi, psi, theta; float Tx[9], Ty[9], Tz[9], Txy[9], T[9]; float Rcom[3]; int i, j; theta = stepSize*((float)rand()/RAND_MAX); phi = stepSize*((float)rand()/RAND_MAX); psi = stepSize*((float)rand()/RAND_MAX); calculateTransformationMatrixRotateXaxis(Tx, theta); calculateTransformationMatrixRotateYaxis(Ty, phi); calculateTransformationMatrixRotateZaxis(Tz, psi); multiplyMatrices(Ty, Tx, Txy, 3, 3, 3); multiplyMatrices(Tz, Txy, T, 3, 3, 3); //for (i=0; i<3; i++) //printf("%f %f %f\n", T[3*i], T[3*i+1], T[3*i+2]); // makeMoleculeWhole(lig, ligNew, pbc); /* for (t=0; t<lig->nAtoms; t++) { printf("CA_CA after makeMoleculeWhole %d ligNew->atom[i].r %f %f %f\n", t, ligNew->atom[t].r[0], ligNew->atom[t].r[1], ligNew->atom[t].r[2]); } printf("CA_CA \n"); */ /* // If molecule is NOT actually whole (ie, if unrealistic bond // lengths), then there's a problem, so exit out of program int lig_res = 330; int lig_atom; int lig_atom_to_print = 0; double CA_CA_bond_dist_in_lig; double del_x_sqrd; double del_y_sqrd; double del_z_sqrd; for (lig_atom=0; lig_atom < ligNew->nAtoms; lig_atom++) { // Print a record of all CA-CA bond distances in the final ligand configuration: if (lig_atom>0) { del_x_sqrd = (ligNew->atom[lig_atom].r[0]-ligNew->atom[lig_atom-1].r[0]) * (ligNew->atom[lig_atom].r[0]-ligNew->atom[lig_atom-1].r[0]); del_y_sqrd = (ligNew->atom[lig_atom].r[1]-ligNew->atom[lig_atom-1].r[1]) * (ligNew->atom[lig_atom].r[1]-ligNew->atom[lig_atom-1].r[1]); del_z_sqrd = (ligNew->atom[lig_atom].r[2]-ligNew->atom[lig_atom-1].r[2]) * (ligNew->atom[lig_atom].r[2]-ligNew->atom[lig_atom-1].r[2]); CA_CA_bond_dist_in_lig = sqrt(del_x_sqrd + del_y_sqrd + del_z_sqrd); printf("CA_CA_bond_dist_in_lig: %f \n", CA_CA_bond_dist_in_lig); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //if (CA_CA_bond_dist_in_lig < 3.6 || CA_CA_bond_dist_in_lig > 4.0) if (CA_CA_bond_dist_in_lig > 4.0) { printf("CA_CA extreme bond dist\n"); exit("\nEXIT\n"); } } lig_res += 1; } printf("CA_CA \n\n\n", t, lig->atom[t].r[0], lig->atom[t].r[1], lig->atom[t].r[2]); */ calculateCenterOfMass(ligNew, Rcom); //printf("ligNew\n"); //for (i=0; i<lig->nAtoms; i++) { //for (j=0; j<3; j++) { //printf("%f ", ligNew->atom[i].r[j]); //} //printf("\n"); //} //printf("Rcom - %f %f %f\n", Rcom[0], Rcom[1], Rcom[2]); for (i=0; i<lig->nAtoms; i++) { for (j=0; j<3; j++) { ligNew->atom[i].r[j] = ligNew->atom[i].r[j] - Rcom[j]; } //printf("i %d ligNew->atom[i].r %f %f %f\n", i, ligNew->atom[i].r[0], ligNew->atom[i].r[1], ligNew->atom[i].r[2]); } /* for (t=0; t<lig->nAtoms; t++) { printf("CA_CA after subtracting Rcoms %d ligNew->atom[i].r %f %f %f\n", t, ligNew->atom[t].r[0], ligNew->atom[t].r[1], ligNew->atom[t].r[2]); } printf("CA_CA \n"); */ rotateCoordinatesTransformationMatrix(ligNew, T); /* for (t=0; t<lig->nAtoms; t++) { printf("CA_CA after rotateCoordinatesTransformationMatrix %d ligNew->atom[i].r %f %f %f\n", t, ligNew->atom[t].r[0], ligNew->atom[t].r[1], ligNew->atom[t].r[2]); } printf("CA_CA \n"); */ for (i=0; i<lig->nAtoms; i++) { for (j=0; j<3; j++) { ligNew->atom[i].r[j] = ligNew->atom[i].r[j] + Rcom[j]; } } /* for (t=0; t<lig->nAtoms; t++) { printf("CA_CA after adding Rcoms %d ligNew->atom[i].r %f %f %f\n", t, ligNew->atom[t].r[0], ligNew->atom[t].r[1], ligNew->atom[t].r[2]); } printf("CA_CA \n"); */ transformCoordinatesToSameBox(ligNew, minR, maxR, pbc); /* for (t=0; t<lig->nAtoms; t++) { printf("CA_CA after transformCoordinatesToSameBox %d ligNew->atom[i].r %f %f %f\n", t, ligNew->atom[t].r[0], ligNew->atom[t].r[1], ligNew->atom[t].r[2]); } */ return; }
void MatrixMultiplication(int sqrtElements) { printf("Starting matrix multiplication\n"); omp_set_num_threads(numThreads); int dimA = 512, dimB = 216; //Size should be a multiple of 8 to avoid segmentation fault error on Xeon Phi if(sqrtElements>0){ dimA = dimB = sqrtElements; } printf("\tMatrixMult, Creating matrices with dimmension %dx%d\n", dimA, dimB); matrix2d *A, *B, *C; A = createMatrix(dimA, dimB); B = createMatrix(dimB, dimA); printf("\tMatrixMult, Randomizing source matrices\n"); randomizeMatrix(A); randomizeMatrix(B); printf("Matrix A:\n"); printMatrix(A, 'd'); printf("Matrix B:\n"); printMatrix(B, 'd'); //__Offload_report(2); printf("\tMatrixMult, Initializing MIC\n"); int nt; nt = omp_get_num_threads(); double totalTime=0, minTime = DBL_MAX, maxTime = 0.; //#pragma offload target(mic:MIC_DEV) \ in(A:length(sizeof(matrix2d*))) \ in(B:length(sizeof(matrix2d*))) \ out(C:length(sizeof(matrix2d*))) #pragma omp parallel { /* warm up to overcome setup overhead */ printf("\tMatrixMult, Test run\n"); C = multiplyMatrices(A, B); // double totalTime=0, minTime = DBL_MAX, maxTime = 0.; struct timeval tvBegin, tvEnd, tvDiff; /*Run matrix multiplication numIterations times and calculate the average running time. */ int i; for (i = 0; i < numIterations; i++) { printf("\tMatrixMult, Starting iter: %d\n", i); gettimeofday(&tvBegin, NULL); C = multiplyMatrices(A, B); gettimeofday(&tvEnd, NULL); double start = tvBegin.tv_sec + ((double)tvBegin.tv_usec/1e6); double end = tvEnd.tv_sec + ((double)tvEnd.tv_usec/1e6); double diff = end - start; maxTime = (maxTime > diff) ? maxTime : diff; minTime = (minTime < diff) ? minTime : diff; totalTime += diff; } } printf("\tMatrixMult, Completed\n"); printf("Product (C):\n"); printMatrix(C, 'd'); double aveTime = totalTime / numIterations; long ops = C->rows * C->cols * C->rows; double gflops = (double)ops * (double)numIterations / ((double)(1e9) * aveTime); printf( "MatrixMult, Summary, "); printf( "%d threads,", numThreads); printf( "%d iterations,", numIterations); printf( "%dx%d matrix,", C->rows, C->cols); printf( "%g maxRT,", maxTime); printf( "%g minRT,",minTime); printf( "%g aveRT,", aveTime); printf( "%g totalRT,", totalTime); printf( "%d operations per iteration,", ops); printf( "%g GFlop/s\n",gflops); deleteMatrix(A); deleteMatrix(B); deleteMatrix(C); //free(A); //free(B); //free(C); //__Offload_report(2); return; }
QJSValue THREEMatrix4::multiply(QJSValue value1, QJSValue value2) { qDebug() << "THREE.Matrix4: .multiply() now only accepts one argument. Use .multiplyMatrices( a, b ) instead."; return multiplyMatrices(value1, value2); }