Created
November 11, 2017 02:41
Star
You must be signed in to star a gist
Example of CUDPP Hash Table
This file contains 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 <stdio.h> | |
#include <cuda_runtime_api.h> | |
#include <cudpp_hash.h> | |
int main() { | |
const int N = 10; | |
// ハッシュテーブルに格納するデータ | |
int keys[N] = {1, 6, 4, 9, 0, 3, 7, 2, 5, 8}; | |
int vals[N] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
// デバイスメモリにコピー | |
int *d_keys, *d_vals; | |
cudaMalloc((void**)&d_keys, sizeof(int) * N); | |
cudaMemcpy(d_keys, keys, sizeof(int) * N, cudaMemcpyHostToDevice); | |
cudaMalloc((void**)&d_vals, sizeof(int) * N); | |
cudaMemcpy(d_vals, vals, sizeof(int) * N, cudaMemcpyHostToDevice); | |
// ハッシュテーブルにクエリするデータ | |
int input[N] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
int output[N]; | |
// デバイスメモリにコピー | |
int *d_input, *d_output; | |
cudaMalloc((void**)&d_input, sizeof(int) * N); | |
cudaMemcpy(d_input, input, sizeof(int) * N, cudaMemcpyHostToDevice); | |
cudaMalloc((void**)&d_output, sizeof(int) * N); | |
cudaMemset(d_output, 0, sizeof(int) * N); | |
// CUDPPの初期化 | |
CUDPPHandle cudpp; | |
cudppCreate(&cudpp); | |
// ハッシュテーブルの設定 | |
CUDPPHashTableConfig config; | |
config.type = CUDPP_BASIC_HASH_TABLE; | |
config.kInputSize = N; | |
config.space_usage = 2.0; | |
// ハッシュテーブルの作成 | |
CUDPPHandle hash_table_handle; | |
cudppHashTable(cudpp, &hash_table_handle, &config); | |
// ハッシュテーブルへデータの格納 | |
cudppHashInsert(hash_table_handle, d_keys, d_vals, N); | |
// ハッシュテーブルへデータのクエリ | |
cudppHashRetrieve(hash_table_handle, d_input, d_output, N); | |
// クエリ結果の確認 | |
cudaMemcpy(output, d_output, sizeof(int) * N, cudaMemcpyDeviceToHost); | |
for (int i = 0; i < N; ++i) { | |
printf("%d\n", output[i]); | |
} | |
// ハッシュテーブルの破棄 | |
cudppDestroyHashTable(cudpp, hash_table_handle); | |
// CUDPPの終了 | |
cudppDestroy(cudpp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment