Question: optimize the below code to acheive 4 . 5 x speed #include #include #include #include #include #define N 1 0 0 0 0 0 0

optimize the below code to acheive 4.5x speed
#include
#include
#include
#include
#include
#define N 1000000000// The upper limit for prime generation
void sieveOfEratosthenesAVX2(int n){
char* sieve =(char*)malloc(n +1);
for (int i =0; i <= n; i++){
sieve[i]=1;
}
int sqrtN =(int)sqrt(n);
for (int p =2; p <= sqrtN; p++){
if (sieve[p]){
__m256i avx_p =_mm256_set1_epi64x((char)p);
for (int i = p * p; i <= n; i += p){
if (i %32==0 && i +31<= n){
// Use AVX2 to unset multiple multiples of p at once
for (int j =0; j <32; j++){
_mm256_storeu_si256((__m256i*)&sieve[i + j],_mm256_and_si256(avx_p,_mm256_setzero_si256()));
}
i +=31;
}
else {
sieve[i]=0;
}
}
}
}
// For measuring time, we only calculate the number of prime numbers (not printing them)
int count =0;
for (int p =2; p <= n; p++){
if (sieve[p]){
count++;
}
}
free(sieve);
}
int main(){
clock_t start_time = clock();
printf("Prime numbers up to %d:
", N);
sieveOfEratosthenesAVX2(N);
clock_t end_time = clock();
double execution_time =(double)(end_time - start_time)/ CLOCKS_PER_SEC;
printf("
Execution time: %f seconds
", execution_time);
return 0;
}
my original code:
#include
#include
#include
#include
#define N 1000000000// The upper limit for prime generation
void sieveOfEratosthenes(int n){
char* sieve =(char*)malloc(n +1);
for (int i =0; i <= n; i++){
sieve[i]=1;
}
int sqrtN =(int)sqrt(n);
for (int p =2; p <= sqrtN; p++){
if (sieve[p]){
for (int i = p * p; i <= n; i += p){
sieve[i]=0;
}
}
}
for (int p =2; p <= n; p++){
if (sieve[p]){
// Print the prime numbers (comment this out if you only want to measure time)
// printf("%d ", p);
}
}
free(sieve);
}
int main(){
clock_t start_time = clock();
printf("Prime numbers up to %d:
", N);
sieveOfEratosthenes(N);
clock_t end_time = clock();
double execution_time =(double)(end_time - start_time)/ CLOCKS_PER_SEC;
printf("
Execution time: %f seconds
", execution_time);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!