回答編集履歴
2
加筆
answer
CHANGED
@@ -1,4 +1,93 @@
|
|
1
1
|
高速化のための tips は皆さんの助言どおり。
|
2
2
|
ですがより速い"アルゴリズム"はなさそうです。
|
3
3
|
ハードウェアの助けを借りて力技でゴリ押すならマルチスレッド(SIMDもアリ)、
|
4
|
-
もっと欲張るならOpenCLとかCUDAとか。
|
4
|
+
もっと欲張るならOpenCLとかCUDAとか。
|
5
|
+
|
6
|
+
[追記] ...ってことで、マルチスレッドとCUDAでやってみたわけよ。
|
7
|
+
|
8
|
+
```C++
|
9
|
+
#include <thread>
|
10
|
+
#include <vector>
|
11
|
+
#include <iostream>
|
12
|
+
#include <chrono>
|
13
|
+
#include <cassert>
|
14
|
+
#include <thread>
|
15
|
+
#include <cuda_runtime.h>
|
16
|
+
#include <device_launch_parameters.h>
|
17
|
+
|
18
|
+
template<typename Function>
|
19
|
+
float time_call(Function&& f) {
|
20
|
+
using namespace std::chrono;
|
21
|
+
auto start = high_resolution_clock::now();
|
22
|
+
f();
|
23
|
+
auto stop = high_resolution_clock::now();
|
24
|
+
return duration_cast<microseconds>(stop - start).count() / 1000.0f;
|
25
|
+
}
|
26
|
+
|
27
|
+
void Addition_single(std::vector<int>& C, const std::vector<int>& A, const std::vector<int>& B, int N) {
|
28
|
+
for ( int i = 0; i < N; ++i ) {
|
29
|
+
C[i] = A[i] + B[i];
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
void Addition_multi(std::vector<int>& C, const std::vector<int>& A, const std::vector<int>& B, int N) {
|
34
|
+
const int nthr = 4;
|
35
|
+
std::thread threads[nthr];
|
36
|
+
for ( int t = 0; t < nthr; ++t ) {
|
37
|
+
threads[t] = std::thread(
|
38
|
+
[&](int begin, int end, int stride) {
|
39
|
+
for ( int i = begin; i < end; i += stride ) {
|
40
|
+
C[i] = A[i] + B[i];
|
41
|
+
}
|
42
|
+
},
|
43
|
+
t, N, nthr);
|
44
|
+
}
|
45
|
+
for ( auto& thr : threads ) thr.join();
|
46
|
+
}
|
47
|
+
|
48
|
+
__global__ void kernel(int* pC, const int* pA, const int* pB, int size) {
|
49
|
+
int i = blockDim.x * blockIdx.x + threadIdx.x;
|
50
|
+
if ( i < size ) {
|
51
|
+
pC[i] = pA[i] + pB[i];
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
void Addition_cuda(std::vector<int>& C, const std::vector<int>& A, const std::vector<int>& B, int N) {
|
56
|
+
int* pA;
|
57
|
+
int* pB;
|
58
|
+
int* pC;
|
59
|
+
cudaMalloc(&pA, N*sizeof(int));
|
60
|
+
cudaMalloc(&pB, N*sizeof(int));
|
61
|
+
cudaMalloc(&pC, N*sizeof(int));
|
62
|
+
cudaMemcpy(pA, A.data(), N*sizeof(int), cudaMemcpyHostToDevice);
|
63
|
+
cudaMemcpy(pB, B.data(), N*sizeof(int), cudaMemcpyHostToDevice);
|
64
|
+
kernel<<<(N+255)/256,256>>>(pC, pA, pB, N);
|
65
|
+
cudaMemcpy(C.data(), pC, N*sizeof(int), cudaMemcpyDeviceToHost);
|
66
|
+
}
|
67
|
+
|
68
|
+
int main() {
|
69
|
+
int N = 2000000;
|
70
|
+
std::vector<int> A(N);
|
71
|
+
std::vector<int> B(N);
|
72
|
+
for ( int i = 0; i < N; ++i ) {
|
73
|
+
A[i] = i; B[i] = -i;
|
74
|
+
}
|
75
|
+
std::vector<int> C(N);
|
76
|
+
|
77
|
+
float single = time_call([&]() { Addition_single(C, A, B, N); });
|
78
|
+
for ( int i = 0; i < N; ++i ) assert( T[i] == 0 );
|
79
|
+
std::cout << "single = " << single << "[ms]\n";
|
80
|
+
|
81
|
+
float multi = time_call([&]() { Addition_multi(C, A, B, N); });
|
82
|
+
for ( int i = 0; i < N; ++i ) assert( T[i] == 0 );
|
83
|
+
std::cout << "multi = " << multi << "[ms]\n" ;
|
84
|
+
|
85
|
+
float cuda = time_call([&]() { Addition_cuda(C, A, B, N); });
|
86
|
+
for ( int i = 0; i < N; ++i ) assert( T[i] == 0 );
|
87
|
+
std::cout << "cuda = " << cuda << "[ms]\n" ;
|
88
|
+
}
|
89
|
+
```
|
90
|
+
|
91
|
+
結果: ぜーんぜん速くならんです。
|
92
|
+
thread/CUDAのオーバヘッドが速くなった分を食ってしまう。
|
93
|
+
時間計算量 O(N) なのを頑張って速くしようと考えん方がむしろ幸せなんじゃないでしょか。
|
1
加筆
answer
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
高速化のための tips は皆さんの助言どおり。
|
2
2
|
ですがより速い"アルゴリズム"はなさそうです。
|
3
|
-
ハードウェアの助けを借りて力技でゴリ押すならマルチスレッド、
|
3
|
+
ハードウェアの助けを借りて力技でゴリ押すならマルチスレッド(SIMDもアリ)、
|
4
4
|
もっと欲張るならOpenCLとかCUDAとか。
|