<jupyter_start><jupyter_text>Test CPU time for matrix multiplication !<jupyter_code>!cat /proc/cpuinfo
!lscpu
%%writefile sam1.cpp

#include <bits/stdc++.h> 
#include <thread>
#include <chrono>

using namespace std; 
  
// maximum size of matrix 
#define MAX 10000 
  
// maximum number of threads 
#define MAX_THREAD 2

int step_i = 0 ; 

float * matA = nullptr ; 
float * matB = nullptr ; 
float * matC = nullptr ;


void* multi(void* arg) 
{ 
    int core = step_i++;

    // std::cout << "core no : " <<  core << std::endl;

    for (int i = core * MAX / MAX_THREAD; i < (core + 1) * MAX / MAX_THREAD; i++)  
    {
        for (int j = 0; j < MAX; j++)  
        { 
           float Pvalue = 0; 
           for (int k = 0; k < MAX ; ++k) {
            float Mdelement = matA[i*MAX + k];
            float Ndelement = matB[k*MAX + j];
            Pvalue += (Mdelement*Ndelement);
            }
            matC[j*MAX + i] = Pvalue ;
        }
    }
} 

int main () {
    matA = (float * )malloc (sizeof(float) * MAX * MAX) ; 
    matB = (float *)malloc (sizeof(float) * MAX * MAX) ; 
    matC = (float *)malloc (sizeof(float) * MAX * MAX) ; 

    for (int i = 0 ; i < MAX * MAX; i++ ) { 
            matA[i]= 1 ;
            matB[i] = 1 ;
    }

    pthread_t threads[MAX_THREAD]; 

    using clock = std::chrono::system_clock;
    using sec = std::chrono::duration<double>;

    const auto before = clock::now();

    for (int i = 0; i < MAX_THREAD; i++) { 
        int* p; 
        pthread_create(&threads[i], NULL, multi, (void*)(p)); 
    } 
  
    for (int i = 0; i < MAX_THREAD; i++)  pthread_join(threads[i], NULL);     

    const sec duration = clock::now() - before;
    std::cout <<  "time to multiply : " << duration.count() << std::endl << std::endl;

    std::cout << matC[1]<< " " << sizeof(matC) / sizeof(matC[0]) << std::endl ;

    delete [] matA ; 
    delete [] matB ;
    delete [] matC ; 
}
%%writefile sam1_vec.cpp

#include <bits/stdc++.h> 
#include <thread> 
#include <chrono> 

using namespace std; 
  
// maximum size of matrix 
#define MAX 10000 
  
// maximum number of threads 
#define MAX_THREAD 2 

int step_i = 0 ; 

float * matA = nullptr ; 
float * matB = nullptr ; 
float * matC = nullptr ;


void* multi(void* arg) 
{ 
    int core = step_i++;
    std::cout << "core no : " <<  core << std::endl;
    for (int i = core * MAX / MAX_THREAD; i < (core + 1) * MAX / MAX_THREAD; i++)  
    {
           float Pvalue = 0; 
           for (int k = 0; k < MAX ; ++k) 
           {
            float Mdelement = matA[i*MAX + k];
            float Ndelement = matB[k];
            Pvalue += (Mdelement*Ndelement);
           }
           matC[i] = Pvalue ;
          // printf("%d \t %f \n",i,Pvalue); 
    }
} 

int main () {
    
    matA = (float * )malloc (sizeof(float) * MAX * MAX) ; 
    matB = (float *)malloc (sizeof(float) * MAX) ; 
    matC = (float *)malloc (sizeof(float) * MAX) ; 

    for (int i = 0 ; i < MAX * MAX; i++ ) { 
        matA[i]= 1 ;
    }

    for (int i = 0 ; i< MAX; i++) { 
        matB[i] = 1; 
    }

    pthread_t threads[MAX_THREAD]; 

    using clock = std::chrono::system_clock;
    using sec = std::chrono::duration<double>;

    const auto before = clock::now();

    for (int i = 0; i < MAX_THREAD; i++) { 
        int* p; 
        pthread_create(&threads[i], NULL, multi, (void*)(p)); 
    } 
  
    for (int i = 0; i < MAX_THREAD; i++)  pthread_join(threads[i], NULL);     

    const sec duration = clock::now() - before;

    std::cout <<  "time to multiply : " << duration.count() << std::endl << std::endl;

    std::cout << matC[1]<< " " << matC[100] << " " << matC[MAX-1] <<  std::endl ;

    delete [] matA ; 
    delete [] matB ;
    delete [] matC ; 
}
%%script bash

g++ sam1_vec.cpp -o test_vec -lpthread
./test_vec<jupyter_output>core no : 0
core no : 1
time to multiply : 0.391942

10000 10000 10000<jupyter_text>Using 4 threads to run the operation<jupyter_code>%%script bash

g++ sam1.cpp -o test -lpthread
./test
%%script bash

g++ sam1.cpp -o test1 -lpthread
./test1<jupyter_output>time to multiply : 13755.5

10000 10000
{'fraction_non_alphanumeric': 0.16224762831427877, 'fraction_numerical': 0.021162734127949404, 'mean_word_length': 2.202492211838006, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '183963', 'license': 'MIT', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1469, 'n_words': 413, 'path': 'benchmark/CPU_threads.ipynb', 'repo_name': 'Aditya-11/High_Speed_Solver', 'size': '4111'}