Advertisement
tannerharvey87

Untitled

Nov 29th, 2020
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. /* CS3339 Assignment 6 Problem 1
  2.  * Handout code, compile using 'g++ -std=c++11 -O3 -Wall -o matrixOps MatrixOps.cpp'
  3.  * Modified by [your name here]
  4.  * Original and Copyright 2020  Lee B. Hinkle
  5.  * Some references used:
  6.  *   https://software.intel.com/en-us/node/529735
  7.  *   http://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/
  8.  *   https://www.techiedelight.com/dynamic-memory-allocation-in-c-for-2d-3d-array/
  9.  * Timing code based off work by Dr. Martin Burtscher, Texas State University
  10.  */
  11. #include <iostream>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <sys/time.h>
  15.  
  16. using namespace std;
  17.  
  18. #define R 10000
  19. #define C 10000
  20. int main()
  21. {
  22.     cout << "Project 6 Array Traversal: ";
  23.     cout << "Integer Array size = " << "FIXME "  << " MegaBytes" << endl;
  24.     int** A = new int*[R]; //allocate pointer for each row
  25.     int** B = new int*[R]; //allocate pointer for each row
  26.  
  27.     for (int i=0; i<C; i++){
  28.         A[i] = new int[R];//allocate each column
  29.         B[i] = new int[R];//allocate each column
  30.     }
  31.  
  32.     //initialize matrix - the destination matrix is not initialized, it won't be in the cache
  33.     for (int r = 0; r < R; r++){
  34.         for (int c = 0; c < C; c++){
  35.             A[r][c] = (int)r*c+1;
  36.         }
  37.     }
  38.  
  39.     double runtime; // for time measurement
  40.     struct timeval start, end;  // for time measurement
  41.    
  42.     gettimeofday(&start, NULL);  // start timer
  43.     // perform matrix operation
  44.     // FIXME -after recording original time, the performance is lousy!!!
  45.     for (int j = 0; j < C; j++){
  46.         for (int i = 0; i < R; i++){
  47.             B[i][j] = 2*A[i][j];
  48.         }
  49.     }
  50.     gettimeofday(&end, NULL);  //end timer
  51.     //sanity check a single element
  52.     if (B[3][4]/A[3][4] == 2){
  53.        printf("B is 2X A, A[3][4] = %d B[3][4] = %d\n", A[3][4], B[3][4]);
  54.     }
  55.     else{
  56.        printf("Error:  B is not 2X A\n");
  57.     }
  58.     //compute and display results
  59.     runtime = end.tv_sec + end.tv_usec / 1000000.0 - start.tv_sec - start.tv_usec / 1000000.0;
  60.     printf("by column (FIXME) compute time: %.4f seconds ", runtime);
  61.     printf("megabytes/sec: %.3f\n", R*C*sizeof(int)*0.000001 / runtime);
  62.    
  63.     //deallocate memory
  64.     for (int i=0; i<C; i++){
  65.         delete[] A[i];
  66.         delete[] B[i];
  67.     }
  68.     delete[] A;
  69.     delete[] B;
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement