diff --git a/.DS_Store b/.DS_Store index 7f2c3626fa7975b0cc99ec4639f8484e750d0428..a25a91b1e8784e41ca95a57840ba1b72224c793c 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/src/radix_sort_par.cu b/src/radix_sort_par.cu index 9a572bafc942e3578d0328579c1485d0aa364a09..61e7d5953f5bc64a0de968d69e3eb99949edc1ea 100644 --- a/src/radix_sort_par.cu +++ b/src/radix_sort_par.cu @@ -47,16 +47,31 @@ void countSort(int arr[], int n, int exp) // The main function to that sorts arr[] of size n using // Radix Sort -void radixsort(int arr[], int n) +void radixsort(int *arr, int n) { + int *d_arr; + // Find the maximum number to know number of digits int m = getMax(arr, n); // Do counting sort for every digit. Note that instead // of passing digit number, exp is passed. exp is 10^i // where i is current digit number + + // allocate device memory + cudaMalloc((void**)&d_arr,sizeof(int)*n); + + cudaMemcpy(d_arr, arr, sizeof(int)*n,cudaMemcpyHostToDevie); for (int exp = 1; m/exp > 0; exp *= 10) - countSort<<1,32>>(arr, n, exp); + countSort<<<1,32>>>(d_arr, n, exp); + + //transfer data back to host memory + cudaMemcpy(arr, d_arr, sizeof(int)*n, cudaMemcpyDeviceToHost); + + //deallocate device memory + cudaFree(d_arr); + + } // A utility function to print an array @@ -89,13 +104,12 @@ timespec diff(timespec start, timespec end) } // Driver program to test above functions -int main() +int main(int argc, char *argv[]) { timespec start, stop; int n; - cout<<"Masukkan nilai N\n"; - cin>>n; + n= atoi(argv[1]); int arr[n]; rng(arr,n); clock_gettime(CLOCK_REALTIME, &start); @@ -107,5 +121,7 @@ int main() long time = duration.tv_sec * 1000000 + duration.tv_nsec/1000; printf("\n%d.%09d s\n", duration.tv_sec, duration.tv_nsec); + //deallocate host memory + free(arr); return 0; } \ No newline at end of file