Created
November 5, 2017 15:34
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compiled with:
Execution times: