Skip to content

Instantly share code, notes, and snippets.

@FedericoPonzi
Created November 5, 2017 15:34
Show Gist options
  • Save FedericoPonzi/789c7df1e0fe6da6a5f32e44a79fa617 to your computer and use it in GitHub Desktop.
Save FedericoPonzi/789c7df1e0fe6da6a5f32e44a79fa617 to your computer and use it in GitHub Desktop.
This is my first attempt to learn CUDA, and is the solution for the problem 21 on project euler.
#include <iostream>
#include <math.h>
#include <vector>
#include <map>
#include <string>
using namespace std;
void divide(int N, long* m){
for(int n = 1; n < N; n++){
m[n] = 0;
for(int i = 1; i < n; i++){
if(n%i == 0){
m[n]+=i;
}
}
}
}
int main(int argc, char** argv){
long* m;
const long k = 100000;
m = (long*) malloc(k * sizeof(long));
divide(k, m);
int res = 0;
for(int i = 1; i < k; i++){
int val = m[i];
if(val < k && val != i && m[val] == i){
res += i;
res += val;
m[i]= 0;
m[val] = 0;
}
}
cout << "res: "<< res << "\n";
return 0;
}
#include <iostream>
#include <math.h>
#include <vector>
#include <map>
#include <string>
#include <stdio.h>
using namespace std;
__global__
void divide(int N, int* m){
int index = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x*gridDim.x;
for(int z = index; z < N; z += stride)
{
m[z] = 0;
for(int id = 1; id < z; id++){
if(z%id == 0){
m[z] +=id;
}
}
}
}
int main(int argc, char** argv){
const int k = 1000000;
int *m;
cudaMallocManaged(&m, k*sizeof(int));
int blockSize= 256;
int numBlocks = (k + blockSize - 1) / blockSize;
divide<<< numBlocks, blockSize>>> (k, m);
cudaDeviceSynchronize();
cout <<"\n";
int res = 0;
for(int i = 1; i < k; i++){
int val = m[i];
if(val < k && val != i && m[val] == i){
res += i;
res += val;
m[i]=0;
m[val]=0;
}
}
cout << "res: "<< res << "\n";
cudaFree(m);
return 0;
}
@FedericoPonzi
Copy link
Author

Compiled with:

g++ 21.cpp -std=c++11 -o 21_cpu
nvcc 21.cu -o 21_gpu

Execution times:

time ./21_cpu 
res: 852810

real	0m16.260s
user	0m16.264s
sys	0m0.004s
time ./21_gpu 
res: 852810

real	0m0.625s
user	0m0.103s
sys	0m0.520s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment