#include <iostream>
#include <Eigen/Core>
#include <Eigen/LU>
#include <boost/compute/function.hpp>
#include <boost/compute/system.hpp>
#include <boost/compute/algorithm/transform.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/types/fundamental.hpp>
namespace compute = boost::compute;
int main()
{
compute::device gpu = compute::system::default_device();
compute::context context(gpu);
compute::command_queue queue(context, gpu);
std::cout << "device: " << gpu.name() << std::endl;
size_t n = 1000;
std::vector<Eigen::Matrix4f> matrices(n);
for(size_t i = 0; i < n; i++){
matrices[i] = Eigen::Matrix4f::Random();
}
using compute::float16_;
compute::vector<float16_> input(n, context);
compute::copy(
matrices.begin(), matrices.end(), input.begin(), queue
);
BOOST_COMPUTE_FUNCTION(float, determinant4x4, (const float16_ m),
{
return m.s0*m.s5*m.sa*m.sf + m.s0*m.s6*m.sb*m.sd + m.s0*m.s7*m.s9*m.se +
m.s1*m.s4*m.sb*m.se + m.s1*m.s6*m.s8*m.sf + m.s1*m.s7*m.sa*m.sc +
m.s2*m.s4*m.s9*m.sf + m.s2*m.s5*m.sb*m.sc + m.s2*m.s7*m.s8*m.sd +
m.s3*m.s4*m.sa*m.sd + m.s3*m.s5*m.s8*m.se + m.s3*m.s6*m.s9*m.sc -
m.s0*m.s5*m.sb*m.se - m.s0*m.s6*m.s9*m.sf - m.s0*m.s7*m.sa*m.sd -
m.s1*m.s4*m.sa*m.sf - m.s1*m.s6*m.sb*m.sc - m.s1*m.s7*m.s8*m.se -
m.s2*m.s4*m.sb*m.sd - m.s2*m.s5*m.s8*m.sf - m.s2*m.s7*m.s9*m.sc -
m.s3*m.s4*m.s9*m.se - m.s3*m.s5*m.sa*m.sc - m.s3*m.s6*m.s8*m.sd;
});
compute::vector<float> determinants(n, context);
compute::transform(
input.begin(), input.end(), determinants.begin(), determinant4x4, queue
);
std::vector<float> host_determinants(n);
compute::copy(
determinants.begin(), determinants.end(), host_determinants.begin(), queue
);
for(size_t i = 0; i < n; i++){
float det = matrices[i].determinant();
if(std::abs(det - host_determinants[i]) > 1e-6){
std::cerr << "error: wrong determinant at " << i << " ("
<< host_determinants[i] << " != " << det << ")"
<< std::endl;
return -1;
}
}
return 0;
}