how to calculate x'Ax while A is coo_matrix

27 views
Skip to first unread message

newHand

unread,
Jun 27, 2016, 3:06:28 PM6/27/16
to cusp-users
Hi
Wish you have a nice day!
I want to calculate x'Ax and x is N x 1 array, A is a N x N matrix.
Because N is very big(about 10,000,000),so I have to make the type of A to be coo_matrix.
I write a test program:

#include <cusp/multiply.h>
#include <cusp/array2d.h>
#include <cusp/print.h>
#include <cusp/multiply.h>

int main(void)
{
const int N = 10000000;
    cusp::coo_matrix<int, float, cusp::host_memory> A(N,N,N);
for (int i = 0; i < N; ++i) {
A.row_indices[i] = i; A.column_indices[i] = i; A.values[i] = i % 10;
}

    cusp::array2d<float, cusp::host_memory> x(1,N);
for (int i = 0; i < N; ++i) {
x(0, i) =  1;
}

    cusp::array2d<float, cusp::host_memory> y(N,1);
for (int i = 0; i < N; ++i) {
y(i, 0) =  1;
}

    cusp::array2d<float, cusp::host_memory> B(1,N);
    cusp::array2d<float, cusp::host_memory> C(1,1);

    cusp::multiply(x, A, B);
    cusp::multiply(B, y, C);

std::cout << C(0, 0) << std::endl;

    return 0;
}

But it didn't pass the compile:

$ nvcc -O2 test.cu -o test

../../cusp/system/detail/generic/multiply.inl(131): error: no instance of overloaded function "cusp::system::detail::generic::multiply" matches the argument list
            argument types are: (cusp::system::cpp::detail::par_t, const cusp::coo_matrix<int, float, cusp::host_memory>, const cusp::array2d<float, cusp::host_memory, cusp::row_major>, cusp::array2d<float, cusp::host_memory, cusp::row_major>, cusp::constant_functor<float>, thrust::multiplies<float>, thrust::plus<float>, Format1, Format2, Format3)
          detected during:
            instantiation of "thrust::detail::disable_if_convertible<UnaryFunction, cusp::known_format, void>::type cusp::system::detail::generic::multiply(thrust::execution_policy<DerivedPolicy> &, const LinearOperator &, const MatrixOrVector1 &, MatrixOrVector2 &, UnaryFunction, BinaryFunction1, BinaryFunction2) [with DerivedPolicy=cusp::system::cpp::detail::par_t, LinearOperator=cusp::coo_matrix<int, float, cusp::host_memory>, MatrixOrVector1=cusp::array2d<float, cusp::host_memory, cusp::row_major>, MatrixOrVector2=cusp::array2d<float, cusp::host_memory, cusp::row_major>, UnaryFunction=cusp::constant_functor<float>, BinaryFunction1=thrust::multiplies<float>, BinaryFunction2=thrust::plus<float>]" 
../../cusp/detail/multiply.inl(78): here
            instantiation of "void cusp::multiply(const thrust::detail::execution_policy_base<DerivedPolicy> &, const LinearOperator &, const MatrixOrVector1 &, MatrixOrVector2 &, UnaryFunction, BinaryFunction1, BinaryFunction2) [with DerivedPolicy=cusp::system::cpp::detail::par_t, LinearOperator=cusp::coo_matrix<int, float, cusp::host_memory>, MatrixOrVector1=cusp::array2d<float, cusp::host_memory, cusp::row_major>, MatrixOrVector2=cusp::array2d<float, cusp::host_memory, cusp::row_major>, UnaryFunction=cusp::constant_functor<float>, BinaryFunction1=thrust::multiplies<float>, BinaryFunction2=thrust::plus<float>]" 
(104): here
            instantiation of "thrust::detail::disable_if_convertible<LinearOperator::format, cusp::unknown_format, void>::type cusp::system::detail::generic::multiply(thrust::execution_policy<DerivedPolicy> &, const LinearOperator &, const MatrixOrVector1 &, MatrixOrVector2 &) [with DerivedPolicy=cusp::system::cpp::detail::par_t, LinearOperator=cusp::coo_matrix<int, float, cusp::host_memory>, MatrixOrVector1=cusp::array2d<float, cusp::host_memory, cusp::row_major>, MatrixOrVector2=cusp::array2d<float, cusp::host_memory, cusp::row_major>]" 
../../cusp/detail/multiply.inl(38): here
            instantiation of "void cusp::multiply(const thrust::detail::execution_policy_base<DerivedPolicy> &, const LinearOperator &, const MatrixOrVector1 &, MatrixOrVector2 &) [with DerivedPolicy=cusp::system::cpp::detail::par_t, LinearOperator=cusp::coo_matrix<int, float, cusp::host_memory>, MatrixOrVector1=cusp::array2d<float, cusp::host_memory, cusp::row_major>, MatrixOrVector2=cusp::array2d<float, cusp::host_memory, cusp::row_major>]" 
../../cusp/detail/multiply.inl(58): here
            instantiation of "void cusp::multiply(const LinearOperator &, const MatrixOrVector1 &, MatrixOrVector2 &) [with LinearOperator=cusp::coo_matrix<int, float, cusp::host_memory>, MatrixOrVector1=cusp::array2d<float, cusp::host_memory, cusp::row_major>, MatrixOrVector2=cusp::array2d<float, cusp::host_memory, cusp::row_major>]" 
test.cu(28): here

1 error detected in the compilation of "/tmp/tmpxft_00003c75_00000000-9_test.cpp1.ii".


I want to ask how can I calculate x'Ax while A is coo_matrix.  Can you write a simple program to tell me the answer please? ( I am stupid and I am afraid I can't understand your answer if you just tell me to use some other funcion in the cusp libary)
 

Steven Dalton

unread,
Jun 27, 2016, 3:15:53 PM6/27/16
to cusp-...@googlegroups.com
Hello,

I have pasted an example of how I would compute x'Ax below. I hope it helps.

Steve

#include <cusp/multiply.h>
#include <cusp/array2d.h>
#include <cusp/print.h>
#include <cusp/multiply.h>

int main(void)
{
    const int N = 1000;
    cusp::coo_matrix<int, float, cusp::host_memory> A(N,N,N);
    for (int i = 0; i < N; ++i) {
        A.row_indices[i] = i;
        A.column_indices[i] = i;
        A.values[i] = i % 10; 
    }   

    cusp::array1d<float, cusp::host_memory> x(N,1);
    cusp::array1d<float, cusp::host_memory> y(N,0);

    cusp::multiply(A, x, y); 
    float b = cusp::blas::dot(y, x); 

    std::cout << b << std::endl;

    return 0;
}

--
You received this message because you are subscribed to the Google Groups "cusp-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cusp-users+...@googlegroups.com.
To post to this group, send email to cusp-...@googlegroups.com.
Visit this group at https://groups.google.com/group/cusp-users.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages