Упорная копия - мажорный столбец OutputIterator
У меня есть вектор матриц (хранится как основные массивы столбцов), который я хочу объединить по вертикали. Поэтому я хочу использовать функцию копирования из структуры Thrust, как в следующем примере фрагмента:
int offset = 0;
for(int i = 0; i < matrices.size(); ++i) {
thrust::copy(
thrust::device_ptr<float>(matrices[i]),
thrust::device_ptr<float>(matrices[i]) + rows[i] * cols[i],
thrust::device_ptr<float>(result) + offset
);
offset += rows[i] * cols[i];
}
РЕДАКТИРОВАТЬ: расширенный пример:
Проблема в том, что если у меня есть матрица A = [[1, 2, 3], [4, 5, 6]] (2 строки, 3 столбца; в памяти [1, 4, 2, 5, 3, 6 ]) и еще один B = [[7, 8, 9]] (1 строка, 3 столбца; в памяти [7, 8, 9]), результирующая матрица C не [[1, 2, 3], [4 , 5, 6], [7, 8, 9]] (3 строки, 3 столбца; в памяти [1, 4, 7, 2, 5, 8, 3, 6, 9]), но [[1, 5 , 7], [4, 3, 8], [2, 6, 9]] (3 строки, 3 столбца; в памяти [1, 4, 2, 5, 3, 6, 7, 8, 9]).
Есть ли способ создать специальный OutputIterator для этой проблемы (я искал его, но ничего не нашел) или быстрый альтернативный способ?
РЕДАКТИРОВАТЬ: SSCCE
#include <thrust/host_vector.h>
#include <thrust/generate.h>
#include <thrust/device_vector.h>
#include <iostream>
void printMat2d(thrust::device_vector<float>& mat, int rows, int cols) {
for(int row = 0; row < rows; ++row) {
for(int col = 0; col < cols; ++col) {
std::cout << mat[row + col * rows] << " ";
}
std::cout << std::endl;
}
}
void printMat1d(thrust::device_vector<float>& mat, int rows, int cols) {
for(int idx = 0; idx < cols*rows; ++idx) {
std::cout << mat[idx] << " ";
}
std::cout << std::endl;
}
void generateMat(thrust::device_vector<float>& mat, int rows, int cols, int add) {
thrust::host_vector<float> matHost(rows * cols);
int val = 0;
for(int row = 0; row < rows; ++row) {
for(int col = 0; col < cols; ++col) {
matHost[row + col * rows] = val + add;
val++;
}
}
mat = matHost;
}
int main() {
std::vector<int> rows(2);
rows[0] = 2;
rows[1] = 3;
std::vector<int> cols(2);
cols[0] = 3;
cols[1] = 3;
//generate matrices
std::vector<thrust::device_vector<float> > matrices(2);
for(size_t i = 0; i < matrices.size(); ++i) {
generateMat(matrices[i], rows[i], cols[i], i*10);
std::cout << "mat_ " << i << " = " << std::endl;
printMat2d(matrices[i], rows[i], cols[i]);
printMat1d(matrices[i], rows[i], cols[i]);
}
//copy
int resultRows = 5;
int resultCols = 3;
thrust::device_vector<float> result(resultRows * resultCols);
int offset = 0;
for(int i = 0; i < matrices.size(); ++i) {
thrust::copy(
matrices[i].begin(),
matrices[i].end(),
result.begin() + offset
);
offset += rows[i] * cols[i];
}
std::cout << "result = " << std::endl;
printMat2d(result, resultRows, resultCols);
printMat1d(result, resultRows, resultCols);
return 0;
}