私は(JNIの助けを借りて)Javaで組み込みの最適化されたマトリックスラッパーを作成しています。これを確認する必要があるのですが、マトリックスの最適化についてヒントをいただけますか? 実装するのは次のとおりです。
行列は、水平アクセス用、垂直アクセス用、対角アクセス用、および必要な場合にのみ行列の要素を計算するためのコマンド バッファの 4 セットのバッファ/配列として表すことができます。これがイラストです。
Matrix signature:
0 1 2 3
4 5 6 7
8 9 1 3
3 5 2 9
First(hroizontal) set:
horSet[0]={0,1,2,3} horSet[1]={4,5,6,7} horSet[2]={8,9,1,3} horSet[3]={3,5,2,9}
Second(vertical) set:
verSet[0]={0,4,8,3} verSet[1]={1,5,9,5} verSet[2]={2,6,1,2} verSet[3]={3,7,3,9}
Third(optional) a diagonal set:
diagS={0,5,1,9} //just in case some calculation needs this
Fourth(calcuation list, in a "one calculation one data" fashion) set:
calc={0,2,1,3,2,5} --->0 means multiply by the next element
1 means add the next element
2 means divide by the next element
so this list means
( (a[i]*2)+3 ) / 5 when only a[i] is needed.
Example for fourth set:
A.mult(2), A.sum(3), A.div(5), A.mult(B)
(to list) (to list) (to list) (calculate *+/ just in time when A is needed )
so only one memory access for four operations.
loop start
a[i] = b[i] * ( ( a[i]*2) +3 ) / 5 only for A.mult(B)
loop end
上記のように、列要素にアクセスする必要がある場合、2 番目のセットは連続したアクセスを提供します。飛躍はありません。水平アクセスの最初のセットでも同じことが達成されました。
これにより、いくつかのことが簡単になり、いくつかのことが難しくなります。
Easier:
**Matrix transpozing operation.
Just swapping the pointers horSet[x] and verSet[x] is enough.
**Matrix * Matrix multiplication.
One matrix gives one of its horizontal set and other matrix gives vertical buffer.
Dot product of these must be highly parallelizable for intrinsics/multithreading.
If the multiplication order is inverse, then horizontal and verticals are switched.
**Matrix * vector multiplication.
Same as above, just a vector can be taken as horizontal or vertical freely.
Harder:
** Doubling memory requirement is bad for many cases.
** Initializing a matrix takes longer.
** When a matrix is multiplied from left, needs an update vertical-->horizontal
sets if its going to be multiplied from right after.(same for opposite)
(if a tranposition is taken between, this does not count)
Neutral:
** Same matrix can be multiplied with two other matrices to get two different
results such as A=A*B(saved in horizontal sets) A=C*A(saved in vertical sets)
then A=A*A gives A*B*C*A(in horizontal) and C*A*A*B (in vertical) without
copying A.
** If a matrix always multiplied from left or always from right, every access
and multiplication will not need update and be contiguous on ram.
** Only using horizontals before transpozing, only using verticals after,
should not break any rules.
主な目的は、(8 の倍数、8 の倍数) サイズのマトリックスを持ち、複数のスレッドで avx 組み込み関数を適用することです (各トレッドはセットで同時に動作します)。
vector * vector dotproduct のみを達成しました。プログラミングの達人が指示を与える場合、私はこれに進みます。
私が書いた(組み込み関数を使用した)ドット積は、ループ展開バージョン(1つずつ乗算する場合の2倍の速度)よりも6倍高速であり、ラッパーでマルチスレッドを有効にすると、メモリ帯域幅の上限でスタックします(8倍->ほぼ20GBを使用) /s は私の ddr3 の限界に近づいています) すでに opencl を試してみましたが、CPU には少し遅いですが、GPU には最適です。
ありがとうございました。
編集:「ブロックマトリックス」バッファはどのように機能しますか? 大きな行列を乗算する場合、小さなパッチは特別な方法で乗算され、メイン メモリ アクセスの削減のためにキャッシュが使用される可能性があります。ただし、これには、垂直 - 水平 - 対角線とこのブロックの間の行列の乗算の間に、さらに多くの更新が必要になります。