I am trying to increase the size of array, I tried with long int, long long int, to store bigger numbers than what int or unsigned int can store. It did not work untill I found the data type 'uint64_t', which worked fine.
Here is the code .
/****************************************************************************** * File: code7.cpp * Author: Vishal * Created: 01/30/24 * Description: This code is supposed to do vector modification for cpu for very large size array of size 2^34 *****************************************************************************/ //This program is intended to demonstrate simple serial programming where a vectors element gets modified serially. // #include <cstdint> #include <iostream> #include <vector> #include <cmath> #include <chrono> using std::cout; using std::endl; void increase_magnitude(float *starting_address,uint64_t size_vec, float mag_multiplyer) { //the variable *starting_address is a pointer which will point to (or contain address of), the first element of the array. for (uint64_t i=0;i<size_vec;i++) //2 instructions per loop for updating i, and checking i<size { //let us multiply the whole vector by 2 //since we have address of first element of vector, we can take its value by use of * *starting_address=*starting_address * mag_multiplyer; //4 instructions //now we shall increase its address by 1. starting_address+=1; //2 instructions (add sum, and update sum) } //in total loop shall have 8 instructions per loop, so time taken shall be (size/clockspeed)*instructions per loop //in this way at the same memory location we will have modified the vector. } long double magnitude_finder(float *starting_address_vec,uint64_t size) { //This function will return the magnitude of the vector. long double sum=0.0; for (uint64_t i=0;i<size;i++) //2 instructions per loop for updating i, and checking i<size { /* sum+=pow((*starting_address_vec),2); //power function is slow (10 times)*/ sum+=(*starting_address_vec)*(*starting_address_vec); //5 instructions starting_address_vec+=1; //2 instructions } //9 instructions per loop return pow(sum,0.5); } long double sum(float *starting_address_vec,uint64_t size) { //This function will return the magnitude of the vector. long double sum=0.0; for (uint64_t i=0;i<size;i++) //2 instructions per loop for updating i, and checking i<size { /* sum+=pow((*starting_address_vec),2); //power function is slow (10 times)*/ sum+=(*starting_address_vec); //5 instructions starting_address_vec+=1; //2 instructions } //9 instructions per loop return sum; } int main(int argc, char *argv[]) { auto time_0 = std::chrono::high_resolution_clock::now(); uint64_t N=1L<<34; //1L denotes long data type of 1. auto time_1 = std::chrono::high_resolution_clock::now(); float *vec=new float[N]; //dynamics_array allocation does not take any time comparable to stack memory //let us fill the vector with N natural numbers(1,2,.......N). auto time_2 = std::chrono::high_resolution_clock::now(); auto elapsed_time_1 = std::chrono::duration_cast<std::chrono::microseconds>(time_2 - time_1).count() / 1e6; auto elapsed_time_storing_N = std::chrono::duration_cast<std::chrono::microseconds>(time_1 - time_1).count() / 1e6; for (uint64_t i=0;i<N;i++) { vec[i]=i+1; } auto time_3 = std::chrono::high_resolution_clock::now(); auto elapsed_time_assigning_values = std::chrono::duration_cast<std::chrono::microseconds>(time_3 - time_2).count() / 1e6; //let us define a pointer to integer vector float *ptr_to_vec=&vec[0]; //giving address of first element. Or we can just write: //we shall modify the vector by multiply it with some real number. //Real numbers are stored in float data types(require 4byte per real number) and double data types(8bytes). float multiplier=4.476; //Let us find the magnitude before changing the vector, long double mag_before=sum(ptr_to_vec,N); auto time_4 = std::chrono::high_resolution_clock::now(); auto elapsed_time_mag1 = std::chrono::duration_cast<std::chrono::microseconds>(time_4 - time_3).count() / 1e6; increase_magnitude(ptr_to_vec,N,multiplier); auto time_5 = std::chrono::high_resolution_clock::now(); auto elapsed_time_modify_mag = std::chrono::duration_cast<std::chrono::microseconds>(time_5 - time_4).count() / 1e6; long double mag_after=sum(ptr_to_vec,N); auto time_6 = std::chrono::high_resolution_clock::now(); auto elapsed_time_mag2 = std::chrono::duration_cast<std::chrono::microseconds>(time_6 - time_5).count() / 1e6; long double ratio_of_magnitudes=mag_after/mag_before; printf("Value of sum before multiplying the vector is %Lf \n",mag_before); printf("Value of sum after multiplying the vector is %Lf \n",mag_after); cout<<"the final vector has sum "<<ratio_of_magnitudes<<" times the earlier one"<<endl<<"which should be equal to the multiplier (which is: "<<multiplier<<")"<<endl; cout<<"the time to assign N: "<<elapsed_time_storing_N<<" seconds"<<endl; cout<<"the time to declare vector of N with doubles data type : "<<elapsed_time_1<<" seconds"<<endl; cout<<"the time to assign vector: "<<elapsed_time_assigning_values<<" seconds"<<endl; cout<<"the time to find sum of vector_in: "<<elapsed_time_mag1<<" seconds"<<endl; cout<<"the time to modify the vector_in: "<<elapsed_time_modify_mag<<" seconds"<<endl; cout<<"the time to calculate sum of modified vector_in: "<<elapsed_time_mag2<<" seconds"<<endl; cout<<" The total time shall be :"<<elapsed_time_1+elapsed_time_assigning_values+elapsed_time_mag1+elapsed_time_modify_mag+elapsed_time_mag2<<" seconds"<<endl; delete[] vec; return 0; }
Output:
```
Value of sum before multiplying the vector is 147573952598266347520.000000
Value of sum after multiplying the vector is 660540987060031083840.000000
the final vector has sum 4.476 times the earlier one
which should be equal to the multiplier (which is: 4.476)
the time to assign N: 0 seconds
the time to declare vector of N with doubles data type : 1e-05 seconds
the time to assign vector: 62.9423 seconds
the time to find sum of vector_in: 94.2152 seconds
the time to modify the vector_in: 40.244 seconds
the time to calculate sum of modified vector_in: 63.339 seconds
The total time shall be :260.741 seconds
real 4m22.348s
user 3m27.879s
sys 0m53.909s
```
Remarks
The answer of sum is not entirely correct, could you find the mistake..............
Think.........Tic...Tic.....Tic.....Tic........
I have already told you.