Skip to content

Instantly share code, notes, and snippets.

@dfukunaga
Created November 11, 2017 02:41
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dfukunaga/7f22df5e54c66a6d6d4b184f5e7ded03 to your computer and use it in GitHub Desktop.
Example of CUDPP Hash Table
#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