Relation Relation::join(Relation r2) { // Combine the schemes and make a new empty relation that uses the combined schemes Relation join_result = Relation("temp_join"); join_result.scheme = join_scheme(r2); // Find which attributes they have in common to prep for the join std::vector<std::pair<int, int>> common_attributes = get_common_attributes(r2); for (Tuple t1 : this->tuples) { for (Tuple t2 : r2.tuples) { if (common_attributes.size() > 0) { // Check if the tuples can join bool join = can_join(t1, t2, common_attributes); if (join) { // Build the joined tuple and add it to the new relation Tuple t = join_tuples(t1, t2, common_attributes); join_result.tuples.insert(t); } } else { Tuple t = do_product(t1, t2); join_result.tuples.insert(t); } } } return join_result; }
/*--------------------------------------------------------------------------- * * Compute matrix product using naive triple-nested loops. * * Input * int argc - length of argv[] array * char* argv[] - pointer to command line parameter array * int verbosity - program verification: verbosity > 0 gives more output * char* order - string indicating loop order, e.g., "ijk" or "jki" * * Output * double - elapsed time for product computation */ double multiply_by_naive_product( int argc, char* argv[], int verbosity, char* order ) { int rows, cols, mids; double **a, **b, **c; double t1, t2; double sec; double gflop_count; /* * process command line arguments */ rows = atoi( argv[0] ); mids = atoi( argv[1] ); cols = atoi( argv[2] ); gflop_count = 2.0 * rows * mids * cols / 1.0e9; if ( verbosity > 0 ) { printf( "naive(%3s): rows = %d, mids = %d, columns = %d\n", order, rows, mids, cols ); } /* * allocate and initialize matrices */ a = (double**) allocateMatrix( rows, mids ); b = (double**) allocateMatrix( mids, cols ); c = (double**) allocateMatrix( rows, cols ); initialize_matrices( a, b, c, rows, cols, mids, verbosity ); /* * compute product */ t1 = wtime(); do_product( a, b, c, 0, rows - 1, 0, cols - 1, 0, mids - 1 ); t2 = wtime(); sec = t2 - t1; if ( verbosity > 1 ) printf( "checksum = %f\n", checksum( c, rows, cols ) ); printf( "naive(%s): %6.3f secs %6.3f gflops ( %5d x %5d x %5d )\n", order, sec, gflop_count / sec, rows, mids, cols ); /* * clean up */ deallocateMatrix( a ); deallocateMatrix( b ); deallocateMatrix( c ); return t2 - t1; }
int main(void){ float num1, num2, result= 0 ; puts( "\nEnter two values: " ); scanf( "%f %f", &num1, &num2 ); result = do_product(num1, num2); printf("The product of a and b is:%f\n", result); return 0; }
/*--------------------------------------------------------------------------- * * Computes block-oriented matrix-matrix product recursively. * * Input: * double** c - matrix product C = A * B * double** a - first factor of product * double** b - second factor of product * int crow, ccol - starting row and column of block of C * int arow, acol - starting row and column of block of A * int brow, bcol - starting row and column of block of B * int l, m, n - dims of blocks: A is l x m, B is m x n, C is l x n * int N - full row length (column dimension) of matrix B * int threshold - B blocks larger than this are partitioned * * Output: * double** c - matrix product C = A * B * * Algorithm based on one presented on page 276 of "Parallel Programming in * C with MPI and OpenMP", Michael J. Quinn, McGraw-Hill, 2004. * * **** NOTE ****: There is a typo in Quinn's code in the recursive call * to mm_rec(). The 5th parameter should be "ccol + nhalf[j]" and not * use "mhalf" as shown in the text. The error only shows up when the * dimensions of the matrices are not uniform. */ void mm_rec( double** c, double** a, double** b, int crow, int ccol, int arow, int acol, int brow, int bcol, int l, int m, int n, int N, int threshold ) { int lhalf[3], mhalf[3], nhalf[3]; int i, j, k; if ( m * n > threshold ) { lhalf[0] = 0; lhalf[1] = l/2; lhalf[2] = l - lhalf[1]; mhalf[0] = 0; mhalf[1] = m/2; mhalf[2] = m - mhalf[1]; nhalf[0] = 0; nhalf[1] = n/2; nhalf[2] = n - nhalf[1]; for ( i = 0; i < 2; i++ ) { for ( j = 0; j < 2; j++ ) { for ( k = 0; k < 2; k++ ) { mm_rec( c, a, b, crow + lhalf[i], ccol + nhalf[j], arow + lhalf[i], acol + mhalf[k], brow + mhalf[k], bcol + nhalf[j], lhalf[i + 1], mhalf[k + 1], nhalf[j + 1], N, threshold ); } } } } else { do_product( a, b, c, arow, arow + l - 1, bcol, bcol + n - 1, acol, acol + m - 1 ); } }
/*--------------------------------------------------------------------------- * * Compute matrix product using tiling. The loop order used for the tile * products is specified in string variable "mode". * * Input * int argc - length of argv[] array * char* argv[] - pointer to command line parameter array * int verbosity - program verification: verbosity > 0 gives more output * char* order - string indicating loop order, e.g., "ijk" or "jki" * * Output * double - elapsed time for product computation */ double multiply_by_tiles( int argc, char* argv[], int verbosity, char* order ) { int rows, cols, mids; int rows_per_tile, cols_per_tile, mids_per_tile; int row_start, row_end; int col_start, col_end; int mid_start, mid_end; double **a, **b, **c; double t1, t2; double sec; double gflop_count; /* * process command line arguments */ rows = atoi( argv[0] ); mids = atoi( argv[1] ); cols = atoi( argv[2] ); rows_per_tile = atoi( argv[3] ); mids_per_tile = atoi( argv[4] ); cols_per_tile = atoi( argv[5] ); gflop_count = 2.0 * rows * mids * cols / 1.0e9; if ( verbosity > 0 ) { printf( "Tiles(%3s): rows = %d, mids = %d, columns = %d\n", order, rows, mids, cols ); printf( "block rows = %d, mids = %d, columns = %d\n", rows_per_tile, mids_per_tile, cols_per_tile ); } /* * allocate and initialize matrices */ a = (double**) allocateMatrix( rows, mids ); b = (double**) allocateMatrix( mids, cols ); c = (double**) allocateMatrix( rows, cols ); initialize_matrices( a, b, c, rows, cols, mids, verbosity ); /* * compute product */ t1 = wtime(); for ( row_start = 0; row_start < rows; row_start += rows_per_tile ) { row_end = row_start + rows_per_tile - 1; if ( row_end >= rows ) row_end = rows - 1; for ( col_start = 0; col_start < cols; col_start += cols_per_tile ) { col_end = col_start + cols_per_tile - 1; if ( col_end >= cols ) col_end = cols - 1; for ( mid_start = 0; mid_start < mids; mid_start += mids_per_tile ) { mid_end = mid_start + mids_per_tile - 1; if ( mid_end >= mids ) mid_end = mids - 1; do_product( a, b, c, row_start, row_end, col_start, col_end, mid_start, mid_end ); } } } t2 = wtime(); sec = t2 - t1; if ( verbosity > 1 ) printf( "checksum = %f\n", checksum( c, rows, cols ) ); printf( "tiles(%3s): %6.3f secs %6.3f gflops ", order, sec, gflop_count / sec ); printf( "( %5d x %5d x %5d ) ( %4d x %4d x %4d )\n", rows, mids, cols, rows_per_tile, mids_per_tile, cols_per_tile ); /* * clean up */ deallocateMatrix( a ); deallocateMatrix( b ); deallocateMatrix( c ); return t2 - t1; }