upgpkg: wordpress 6.2.1-1
[ArchLinux/community.git] / rocsolver / trunk / test.cpp
blobe7968fa54d555b24ac33fe0992841d7567a77db6
1 #include <rocsolver/rocsolver.h>
2 #include <hip/hip_runtime.h>
3 #include <vector>
4 #include <random>
5 #include <algorithm>
6 #include <cmath>
7 #include <iostream>
9 int main()
11 size_t n = 128;
12 size_t size = n * n;
14 std::random_device rd;
15 std::mt19937 gen(rd());
16 std::uniform_real_distribution<float> dist(-1.0, 1.0);
17 auto myrand = [&](){return dist(gen);};
19 float *a;
20 float *x;
21 hipMalloc((void**)&a, sizeof *a * size);
22 hipMalloc((void**)&x, sizeof *x * n);
24 std::vector<float> ain(size);
25 std::vector<float> xin(n);
26 std::generate(ain.begin(), ain.end(), myrand);
27 std::generate(xin.begin(), xin.end(), myrand);
29 hipMemcpy(a, ain.data(), sizeof *a * size, hipMemcpyHostToDevice);
30 hipMemcpy(x, xin.data(), sizeof *x * n, hipMemcpyHostToDevice);
32 rocblas_handle handle;
33 rocblas_create_handle(&handle);
35 rocblas_int *info;
36 hipMalloc((void**)&info, sizeof *info);
37 rocsolver_sgels(handle, rocblas_operation_none,
38 n, n, 1, a, n, x, n, info);
40 std::vector<float> xout(n);
41 hipMemcpy(xout.data(), x, sizeof *x * n, hipMemcpyDeviceToHost);
42 rocblas_int hinfo;
43 hipMemcpy(&hinfo, info, sizeof *info, hipMemcpyDeviceToHost);
45 if(hinfo != 0){
46 std::cout << "Matrix is rank deficient!\n";
47 return 1;
50 float tol = 0.001f;
51 for(size_t i = 0; i < n; i++){
52 for(size_t j = 0; j < n; j++){
53 xin[i] -= ain[i + j * n] * xout[j];
55 if(std::abs(xin[i]) > tol){
56 std::cout << "Missmatch at index " << i << "\n"
57 << "Desired: 0" << "\n"
58 << "Actual : " << xin[i] << std::endl;
59 return 1;
63 std::cout << "TESTS PASSED!" << std::endl;
65 hipFree(a);
66 hipFree(x);
67 rocblas_destroy_handle(handle);