main.cc 1.47 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <hash/hash_table.h>
#include <cuda.h>

int main(){
    auto table = cudahash::HashTable();
    table.Initialize(10, 2.0);
    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);
    bool s = table.Build(N, (const unsigned int *) d_keys, 
                            (const unsigned int *) d_vals);

    std::cout << s << std::endl;
    table.Retrieve(N, (const unsigned int *) d_input,
                            (unsigned int *) d_output);

    std::cout << s << std::endl;
    cudaMemcpy(output, d_output, sizeof(int) * N, cudaMemcpyDeviceToHost);
    for (int i = 0; i < N; ++i) {
        printf("%d\n", output[i]);
    }

    return 0;
}