From 6712dde5f7cc3baf03f9e16cfb775ebe1039fddf Mon Sep 17 00:00:00 2001 From: Putu Gde Aditya Taguh Widiana <13517032@std.stei.itb.ac.id> Date: Sat, 28 Mar 2020 19:50:59 +0700 Subject: [PATCH] updated dijkstra cuda --- djikstra_cuda.cu | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/djikstra_cuda.cu b/djikstra_cuda.cu index 6380533..eb8be35 100644 --- a/djikstra_cuda.cu +++ b/djikstra_cuda.cu @@ -4,13 +4,13 @@ #include <stdbool.h> #include <sys/time.h> #include <string.h> -// #include <cuda.h> +#include <cuda.h> -#define INFINITY INT_MAX; #define MIN(a,b) (((a)<(b))?(a):(b)); #define MAX(a,b) (((a)>(b))?(a):(b)); -void createGraph(int n, int graph[n]){ +__host__ +void createGraph(int n, int *graph){ srand(13517032); int val; for(int i=0;i<n;i++){ @@ -23,7 +23,7 @@ void createGraph(int n, int graph[n]){ } } -void printGraph(int n, int graph[n]){ +void printGraph(int n, int *graph){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ printf("%d ",graph[i*n+j]); @@ -38,9 +38,10 @@ void djikstra(int graph[],int start,int n,int answer[]){ int node; int temp_dist; - int *distance,*visited; - distance = malloc(n*sizeof(int)); - visited = malloc(n*sizeof(int)); + // distance = malloc(n*sizeof(int)); + // visited = malloc(n*sizeof(int)); + int *distance = new int [n]; + int *visited = new int [n]; int min_dist[2]; // node and its distance min_dist[0] = -1; min_dist[1] = INFINITY; @@ -113,9 +114,13 @@ int main(int argc, char** argv){ strcat(filename,".txt"); output_file = fopen(filename, "w+"); - int *graph,*distance,*visited, *answer; - graph = malloc(n*n*sizeof(int)); - answer = malloc(n*n*sizeof(int)); + int *graph, *answer; + // graph = malloc(n*n*sizeof(int)); + // answer = malloc(n*n*sizeof(int)); + + cudaMallocManaged(&graph,n*n*sizeof(int)); + cudaMallocManaged(&answer,n*n*sizeof(int)); + createGraph(n,graph); fprintf(output_file, "GRAPH:\n"); for(int i = 0;i<n;i++){ @@ -130,8 +135,11 @@ int main(int argc, char** argv){ int numBlocks = (n + blockSize - 1) / blockSize; gettimeofday(&st,NULL); - djikstra_cuda<<<numBlocks, blockSize>>>>(graph,n,answer); + + djikstra_cuda<<<numBlocks, blockSize>>>(graph,n,answer); + gettimeofday(&et,NULL); + cudaDeviceSynchronize(); unsigned long long int elapsed = ((et.tv_sec - st.tv_sec) * 1000000) + (et.tv_usec - st.tv_usec); printf("Djikstra total time: %llu micro seconds\n",elapsed); fprintf(output_file, "Answer:\n"); @@ -141,6 +149,10 @@ int main(int argc, char** argv){ } fprintf(output_file, "\n"); } + + cudaFree(graph); + cudaFree(answer); + return 0; } -- GitLab