28,347 views
2 votes
2 votes
(3 points) Write a program to process two large chunks of data (e.g., a large 3D array and an array of self-defined structures with each structure at least 256 Bytes) respectively. The array elements can be random. The process can be incrementing every element by 1 or others. Try different ways (e.g., stride-k reference, the loop orders or others) to traverse the array elements and simulate the program performance (i.e., running time). Note that you should record the time just before and after the data processing program, not including the data generation or other initiation programs. And you should calculate an average time of at least 5 experiments for each program.

User Jared Mackey
by
2.8k points

2 Answers

1 vote
1 vote

Answer:

See explaination

Step-by-step explanation:

#include <iostream> #include <chrono> using namespace std::chrono; using namespace std; struct X{ int a,b,c,d,e,f,g,h; }; int main(){ // 3D Array of integers 1000 x 1000 x 1000 int data1[10][10][10]; //Array of struct X struct X data2[10000] ; auto start = high_resolution_clock::now(); //stride 1 access data 1 Loop order 1: for(int i=0;i<1000;i++){ for(int j=0;j<1000;j++){ for(int k=0;k<1000;k++){ data1[i][j][k]; } } } auto stop = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(stop - start); cout<<"3D array Stride 1 Loop Order 1"<<duration.count()<<endl; start = high_resolution_clock::now(); //stride 2 access data 1 Loop order 1: for(int i=0;i<1000;i+=2){ for(int j=0;j<1000;j+=2){ for(int k=0;k<1000;k+=2){ data1[i][j][k]; } } } stop = high_resolution_clock::now(); duration = duration_cast<microseconds>(stop - start); cout<<"3D array Stride 2 Loop Order 1"<<duration.count()<<endl; start = high_resolution_clock::now(); //stride 1 access data 1 Loop order 2: for(int i=0;i<1000;i++){ for(int j=0;j<1000;j++){ for(int k=0;k<1000;k++){ data1[j][i][k]; } } } stop = high_resolution_clock::now(); duration = duration_cast<microseconds>(stop - start); cout<<"3D array Stride 1 Loop Order 2"<<duration.count()<<endl; start = high_resolution_clock::now(); for(int i=0;i<10000;i++){ data2[i]; } stop = high_resolution_clock::now(); duration = duration_cast<microseconds>(stop - start); cout<<"Struct Array "<<duration.count()<<endl; }

Kindly check attachment for screenshot of the source code for proper indentation.

(3 points) Write a program to process two large chunks of data (e.g., a large 3D array-example-1
(3 points) Write a program to process two large chunks of data (e.g., a large 3D array-example-2
User Alextk
by
3.4k points
5 votes
5 votes

Answer:

Check the explanation

Step-by-step explanation:

#include <iostream> #include <chrono> using namespace std::chrono; using namespace std; struct X{ int a,b,c,d,e,f,g,h; }; int main(){ // 3D Array of integers 1000 x 1000 x 1000 int data1[10][10][10]; //Array of struct X struct X data2[10000] ; auto start = high_resolution_clock::now(); //stride 1 access data 1 Loop order 1: for(int i=0;i<1000;i++){ for(int j=0;j<1000;j++){ for(int k=0;k<1000;k++){ data1[i][j][k]; } } } auto stop = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(stop - start); cout<<"3D array Stride 1 Loop Order 1"<<duration.count()<<endl; start = high_resolution_clock::now(); //stride 2 access data 1 Loop order 1: for(int i=0;i<1000;i+=2){ for(int j=0;j<1000;j+=2){ for(int k=0;k<1000;k+=2){ data1[i][j][k]; } } } stop = high_resolution_clock::now(); duration = duration_cast<microseconds>(stop - start); cout<<"3D array Stride 2 Loop Order 1"<<duration.count()<<endl; start = high_resolution_clock::now(); //stride 1 access data 1 Loop order 2: for(int i=0;i<1000;i++){ for(int j=0;j<1000;j++){ for(int k=0;k<1000;k++){ data1[j][i][k]; } } } stop = high_resolution_clock::now(); duration = duration_cast<microseconds>(stop - start); cout<<"3D array Stride 1 Loop Order 2"<<duration.count()<<endl; start = high_resolution_clock::now(); for(int i=0;i<10000;i++){ data2[i]; } stop = high_resolution_clock::now(); duration = duration_cast<microseconds>(stop - start); cout<<"Struct Array "<<duration.count()<<endl; }

Some Observations on the order:

Stride 1 goes over all the elements of the array Hence takes more time than stride 2 which goes over alternate elements.

Loop order in the row major form takes leads time than column major form!

Struct array takes no time to execute because the structs are not being accessed.

Check the code screenshot and code output in the attached image below.

(3 points) Write a program to process two large chunks of data (e.g., a large 3D array-example-1
(3 points) Write a program to process two large chunks of data (e.g., a large 3D array-example-2
(3 points) Write a program to process two large chunks of data (e.g., a large 3D array-example-3
User DarrylG
by
3.0k points