Menu

compile errors with import_vector to ViennaCL function

tario
2017-08-29
2017-08-29
  • tario

    tario - 2017-08-29

    Hello,

    I wrote a function to import vectors to ViennaCL. For now it's supposed to work on the host only.

    I have been going in circles with type mismatches / conversion errors for quite a while but I am not able to resolve them. In addition to that, freeing the memory doesn't work.

    What are the required types and how to free the memory?

    The code:

    // import_vector(from, to) e.g. from Foam to ViennaCL 
    void import_vector(const Foam::scalarField &foam_vec, viennacl::vector<ScalarType> &vcl_vec) 
    {
        vcl_vec.clear();
        uint n = foam_vec.size();
        // Get pointers for vectors containing solution (psi) or right hand side (source)
        const ScalarType* values = foam_vec.begin(); // 
    
        // Allocate memory for vector
        vcl_vec = (ScalarType *)calloc(n, sizeof(ScalarType)); // error
    
        // Fill vector
        for (uint i=0; i < n; i++)
        {
        vcl_vec[i] = values[i];
        }
        // Free and release memory of vector
        free(values); // error 
    }
    

    The compile errors:

    clFOAM.C:96:16: error: invalid conversion from ‘const void’ to ‘void[-fpermissive]
    free(values); // error
    ^
    In file included from /usr/include/c++/5/cstdlib:72:0,
    from /opt/openfoam5/src/OpenFOAM/lnInclude/int32.H:41,
    from /opt/openfoam5/src/OpenFOAM/lnInclude/int.H:38,
    from /opt/openfoam5/src/OpenFOAM/lnInclude/label.H:39,
    from /opt/openfoam5/src/OpenFOAM/lnInclude/labelList.H:47,
    from /opt/openfoam5/src/OpenFOAM/lnInclude/lduAddressing.H:99,
    from /opt/openfoam5/src/OpenFOAM/lnInclude/lduMesh.H:36,
    from /opt/openfoam5/src/OpenFOAM/lnInclude/lduMatrix.H:53,
    from clFOAM.H:4,
    from clFOAM.C:4:
    /usr/include/stdlib.h:483:13: note: initializing argument 1 of ‘void free(void)’
    extern void free (void
    ptr) THROW;
    ^
    In file included from lnInclude/../viennacl/compressed_matrix.hpp:29:0,
    from clFOAM.H:8,
    from clFOAM.C:4:
    ./viennacl/vector.hpp: In instantiation of ‘viennacl::vector<SCALARTYPE, ALIGNMENT="">::self_type& viennacl::vector<SCALARTYPE, ALIGNMENT="">::operator=(const T&) [with T = double*; NumericT = double; unsigned int AlignmentV = 1u; viennacl::vector<SCALARTYPE, ALIGNMENT="">::self_type = viennacl::vector<double>]’:
    clFOAM.C:88:13: required from here
    ./viennacl/vector.hpp:1033:25: error: no matching function for call to ‘viennacl::vector<double>::operator=(double const&)’
    base_type::operator=(other);
    ^
    ./viennacl/vector.hpp:356:43: note: candidate: viennacl::vector_base<NumericT, SizeT,="" DistanceT="">& viennacl::vector_base<SCALARTYPE, SizeType,="" DistanceType="">::operator=(const self_type&) [with NumericT = double; SizeT = long unsigned int; DistanceT = long int; viennacl::vector_base<SCALARTYPE, SizeType,="" DistanceType="">::self_type = viennacl::vector_base<double, long="" unsigned="" int,="" long="" int="">]
    vector_base<NumericT, SizeT,="" DistanceT=""> & vector_base<NumericT, SizeT,="" DistanceT="">::operator=(const self_type & vec)
    ^
    ./viennacl/vector.hpp:356:43: note: no known conversion for argument 1 from ‘double
    const’ to ‘const self_type& {aka const viennacl::vector_base<double, long="" unsigned="" int,="" long="" int="">&}’
    In file included from ./viennacl/vector.hpp:27:0,
    from lnInclude/../viennacl/compressed_matrix.hpp:29,
    from clFOAM.H:8,
    from clFOAM.C:4:
    ./viennacl/detail/vector_def.hpp:181:15: note: candidate: template<class LHS,="" class="" RHS,="" class="" OP=""> viennacl::vector_base<NumericT, SizeT,="" DistanceT="">& viennacl::vector_base<SCALARTYPE, SizeType,="" DistanceType="">::operator=(const viennacl::vector_expression<const LHS,="" const="" RHS,="" OP="">&) [with LHS = LHS; RHS = RHS; OP = OP; NumericT = double; SizeT = long unsigned int; DistanceT = long int]
    self_type & operator=(const vector_expression<const LHS,="" const="" RHS,="" OP=""> & proxy);
    ^
    ./viennacl/detail/vector_def.hpp:181:15: note: template argument deduction/substitution failed:
    In file included from lnInclude/../viennacl/compressed_matrix.hpp:29:0,
    from clFOAM.H:8,
    from clFOAM.C:4:
    ./viennacl/vector.hpp:1033:25: note: mismatched types ‘const viennacl::vector_expression<const T1,="" const="" T2,="" OP="">’ and ‘double const’
    base_type::operator=(other);
    ^
    In file included from ./viennacl/vector.hpp:27:0,
    from lnInclude/../viennacl/compressed_matrix.hpp:29,
    from clFOAM.H:8,
    from clFOAM.C:4:
    ./viennacl/detail/vector_def.hpp:184:16: note: candidate: template<class OtherNumericT=""> viennacl::vector_base<NumericT, SizeT,="" DistanceT="">& viennacl::vector_base<SCALARTYPE, SizeType,="" DistanceType="">::operator=(const viennacl::vector_base<OtherNumericT>&) [with OtherNumericT = OtherNumericT; NumericT = double; SizeT = long unsigned int; DistanceT = long int]
    self_type & operator = (const vector_base<OtherNumericT> & v1);
    ^
    ./viennacl/detail/vector_def.hpp:184:16: note: template argument deduction/substitution failed:
    In file included from lnInclude/../viennacl/compressed_matrix.hpp:29:0,
    from clFOAM.H:8,
    from clFOAM.C:4:
    ./viennacl/vector.hpp:1033:25: note: mismatched types ‘const viennacl::vector_base<T>’ and ‘double
    const’
    base_type::operator=(other);
    ^
    ./viennacl/vector.hpp:434:43: note: candidate: viennacl::vector_base<NumericT, SizeT,="" DistanceT="">& viennacl::vector_base<SCALARTYPE, SizeType,="" DistanceType="">::operator=(const viennacl::unit_vector<NumericT>&) [with NumericT = double; SizeT = long unsigned int; DistanceT = long int]
    vector_base<NumericT, SizeT,="" DistanceT=""> & vector_base<NumericT, SizeT,="" DistanceT="">::operator = (unit_vector<NumericT> co
    ^
    ./viennacl/vector.hpp:434:43: note: no known conversion for argument 1 from ‘double const’ to ‘const viennacl::unit_vector<double>&’
    ./viennacl/vector.hpp:460:43: note: candidate: viennacl::vector_base<NumericT, SizeT,="" DistanceT="">& viennacl::vector_base<SCALARTYPE, SizeType,="" DistanceType="">::operator=(const viennacl::zero_vector<NumericT>&) [with NumericT = double; SizeT = long unsigned int; DistanceT = long int] <near match="">
    vector_base<NumericT, SizeT,="" DistanceT=""> & vector_base<NumericT, SizeT,="" DistanceT="">::operator = (zero_vector<NumericT> co
    ^
    ./viennacl/vector.hpp:460:43: note: conversion of argument 1 would be ill-formed:
    ./viennacl/vector.hpp:1033:25: error: invalid user-defined conversion from ‘double
    const’ to ‘const viennacl::zero_vector<double>&’ [-fpermissive]
    base_type::operator=(other);
    ^
    In file included from ./viennacl/vector.hpp:27:0,
    from lnInclude/../viennacl/compressed_matrix.hpp:29,
    from clFOAM.H:8,
    from clFOAM.C:4:
    ./viennacl/detail/vector_def.hpp:95:3: note: candidate is: viennacl::zero_vector<SCALARTYPE>::zero_vector(viennacl::vcl_size_t, viennacl::context) [with NumericT = double; viennacl::vcl_size_t = long unsigned int] <near match="">
    zero_vector(vcl_size_t s, viennacl::context ctx = viennacl::context()) : scalar_vector<NumericT>(s, 0, ctx){}
    ^
    ./viennacl/detail/vector_def.hpp:95:3: note: conversion of argument 1 would be ill-formed:
    In file included from lnInclude/../viennacl/compressed_matrix.hpp:29:0,
    from clFOAM.H:8,
    from clFOAM.C:4:
    ./viennacl/vector.hpp:1033:25: error: invalid conversion from ‘double’ to ‘viennacl::vcl_size_t {aka long unsigned int}’ [-fpermissive]
    base_type::operator=(other);
    ^
    ./viennacl/vector.hpp:1033:25: error: invalid conversion from ‘double
    ’ to ‘viennacl::vcl_size_t {aka long unsigned int}’ [-fpermissive]
    In file included from ./viennacl/vector.hpp:27:0,
    from lnInclude/../viennacl/compressed_matrix.hpp:29,
    from clFOAM.H:8,
    from clFOAM.C:4:
    ./viennacl/detail/vector_def.hpp:95:3: note: initializing argument 1 of ‘viennacl::zero_vector<SCALARTYPE>::zero_vector(viennacl::vcl_size_t, viennacl::context) [with NumericT = double; viennacl::vcl_size_t = long unsigned int]
    zero_vector(vcl_size_t s, viennacl::context ctx = viennacl::context()) : scalar_vector<NumericT>(s, 0, ctx){}
    ^
    In file included from lnInclude/../viennacl/compressed_matrix.hpp:29:0,
    from clFOAM.H:8,
    from clFOAM.C:4:
    ./viennacl/vector.hpp:483:43: note: candidate: viennacl::vector_base<NumericT, SizeT,="" DistanceT="">& viennacl::vector_base<SCALARTYPE, SizeType,="" DistanceType="">::operator=(const viennacl::scalar_vector<NumericT>&) [with NumericT = double; SizeT = long unsigned int; DistanceT = long int]
    vector_base<NumericT, SizeT,="" DistanceT=""> & vector_base<NumericT, SizeT,="" DistanceT="">::operator = (scalar_vector<NumericT>
    ^
    ./viennacl/vector.hpp:483:43: note: no known conversion for argument 1 from ‘double const’ to ‘const viennacl::scalar_vector<double>&’
    ./viennacl/vector.hpp:514:43: note: candidate: viennacl::vector_base<NumericT, SizeT,="" DistanceT="">& viennacl::vector_base<SCALARTYPE, SizeType,="" DistanceType="">::operator=(const viennacl::vector_expression<const viennacl::matrix_base<T="">, const viennacl::vector_base<T>, viennacl::op_prod>&) [with NumericT = double; SizeT = long unsigned int; DistanceT = long int]
    vector_base<NumericT, SizeT,="" DistanceT=""> & vector_base<NumericT, SizeT,="" DistanceT="">::operator=(const viennacl::vector_exp
    ^
    ./viennacl/vector.hpp:514:43: note: no known conversion for argument 1 from ‘double
    const’ to ‘const viennacl::vector_expression<const viennacl::matrix_base<double,="" long="" unsigned="" int,="" long="" int="">, const viennacl::vector_base<double, long="" unsigned="" int,="" long="" int="">, viennacl::op_prod>&’
    ./viennacl/vector.hpp:535:43: note: candidate: viennacl::vector_base<NumericT, SizeT,="" DistanceT="">& viennacl::vector_base<SCALARTYPE, SizeType,="" DistanceType="">::operator=(const viennacl::vector_expression<const viennacl::matrix_expression<const="" viennacl::matrix_base<T="">, const viennacl::matrix_base<T>, viennacl::op_trans>, const viennacl::vector_base<T>, viennacl::op_prod>&) [with NumericT = double; SizeT = long unsigned int; DistanceT = long int]
    vector_base<NumericT, SizeT,="" DistanceT=""> & vector_base<NumericT, SizeT,="" DistanceT="">::operator=(const vector_expression< c
    ^
    ./viennacl/vector.hpp:535:43: note: no known conversion for argument 1 from ‘double const’ to ‘const viennacl::vector_expression<const viennacl::matrix_expression<const="" viennacl::matrix_base<double,="" long="" unsigned="" int,="" long="" int="">, const viennacl::matrix_base<double, long="" unsigned="" int,="" long="" int="">, viennacl::op_trans>, const viennacl::vector_base<double, long="" unsigned="" int,="" long="" int="">, viennacl::op_prod>&’
    /opt/openfoam5/wmake/rules/General/transform:25: die Regel für Ziel „Make/linux64GccDPInt32Opt/clFOAM.o“ scheiterte
    make:
    ** [Make/linux64GccDPInt32Opt/clFOAM.o] Fehler 1

     
  • Karl Rupp

    Karl Rupp - 2017-08-30

    Hi Tario,

    I think you are looking for viennacl::fast_copy():

    // import_vector(from, to) e.g. from Foam to ViennaCL 
    void import_vector(const Foam::scalarField &foam_vec, viennacl::vector<ScalarType> &vcl_vec) 
    {
        vcl_vec.clear();
        uint n = foam_vec.size();
        // Get pointers for vectors containing solution (psi) or right hand side (source)
        const ScalarType* values = foam_vec.begin(); // 
    
        // Allocate memory for vector
        viennacl::fast_copy(values, values + n, vcl_vec.begin());
    }
    

    For copying from ViennaCL to Foam, you'll need

    viennacl::fast_copy(vcl_vec.begin(), vcl_vec.end(), values);
    

    with a non-const pointer values. Make sure that vcl_vec has the correct size n before copying values.

    Best regards,
    Karli

     
  • tario

    tario - 2017-08-30

    here's an update, the errors are now down to type issues:

    void import_vector
    (
        const Foam::scalarField &foam_vec, 
        viennacl::vector<ScalarType> &vcl_vec
    ) 
    {
        vcl_vec.clear();
        uint n = foam_vec.size();
    
        // Get pointers for vectors containing solution (psi) or right hand side (source)
        const ScalarType * values = foam_vec.begin();
    
        // Allocate memory for vector
        *vcl_vec = (ScalarType *)calloc(n, sizeof(ScalarType*)); // error seems to arise here with memory allocation
    
        // Fill vector
        for (uint i=0; i < n; i++)
        {
            vcl_vec[i] = values[i];
        }
    
        // Free and release memory of vector
        delete [] values; // instead of free(values), pointers e.g to arrays must be deleted!
    }
    
     
  • Karl Rupp

    Karl Rupp - 2017-08-30

    We've been posting at almost the same time - see my solution recommendation above. :-)

     
  • tario

    tario - 2017-08-30

    I get an error, maybe based on a misunderstanding, import means from Foam importing into ViennaCL, I want to fill the ViennaCL vector. (But I'll have to export the result too.)

    The error is:

    In file included from lnInclude/../viennacl/compressed_matrix.hpp:29:0,
                     from clFOAM.H:8,
                     from clFOAM.C:4:
    ./viennacl/vector.hpp: In instantiation of void viennacl::fast_copy(const viennacl::const_vector_iterator<SCALARTYPE, ALIGNMENT_SRC>&, const viennacl::const_vector_iterator<SCALARTYPE, ALIGNMENT_SRC>&, CPU_ITERATOR) [with SCALARTYPE = double; unsigned int ALIGNMENT = 1u; CPU_ITERATOR = const double*]:
    clFOAM.C:98:63:   required from here
    ./viennacl/vector.hpp:1242:37: error: invalid conversion from const void* to void* [-fpermissive]
           viennacl::backend::memory_read(gpu_begin.handle(),
                                         ^
    In file included from ./viennacl/scalar.hpp:28:0,
                     from ./viennacl/tools/entry_proxy.hpp:27,
                     from ./viennacl/detail/vector_def.hpp:26,
                     from ./viennacl/vector.hpp:27,
                     from lnInclude/../viennacl/compressed_matrix.hpp:29,
                     from clFOAM.H:8,
                     from clFOAM.C:4:
    ./viennacl/backend/memory.hpp:261:15: note:   initializing argument 4 of void viennacl::backend::memory_read(const viennacl::backend::mem_handle&, viennacl::vcl_size_t, viennacl::vcl_size_t, void*, bool)
       inline void memory_read(mem_handle const & src_buffer,
                   ^
    In file included from lnInclude/../viennacl/compressed_matrix.hpp:29:0,
                     from clFOAM.H:8,
                     from clFOAM.C:4:
    ./viennacl/vector.hpp:1255:28: error: assignment of read-only location *(cpu_begin + ((sizetype)(i * 8ul)))
             (&(*cpu_begin))[i] = temp_buffer[i * gpu_begin.stride()];
                                ^
    /opt/openfoam5/wmake/rules/General/transform:25: die Regel für Ziel Make/linux64GccDPInt32Opt/clFOAM.o scheiterte
    make: *** [Make/linux64GccDPInt32Opt/clFOAM.o] Fehler 1
    
     
  • Karl Rupp

    Karl Rupp - 2017-08-30

    Did you use viennacl::fast_copy(values, values + n, vcl_vec.begin()); for copying from Foam to ViennaCL? Your error message suggests something else.

     
  • tario

    tario - 2017-08-30

    Like this?

    void import_vector
    (
        const Foam::scalarField &foam_vec, 
        viennacl::vector<ScalarType> &vcl_vec
    ) 
    {
        vcl_vec.clear();
        uint n = foam_vec.size();
    
        // Get pointers for vector containing right hand side (source)
        const ScalarType * values = foam_vec.begin(); // not sure about this
    
        // Load right hand side (source) into ViennaCL vector
        viennacl::fast_copy(values, values + n, vcl_vec.begin());
    
        // Free and release memory of vector
        delete [] values; // instead of free(values), pointers e.g to arrays must be deleted!
    }
    
     
  • Karl Rupp

    Karl Rupp - 2017-08-30

    You don't need to free the pointer, because no allocation is happening when you call foam_vec.begin(). Cleaning up, you can actually write things in a single line:

    void import_vector
    (
        const Foam::scalarField &foam_vec, 
        viennacl::vector<ScalarType> &vcl_vec
    ) 
    {
        // Load right hand side (source) into ViennaCL vector
        viennacl::copy(foam_vec.begin(), foam_vec.end(), vcl_vec.begin());
    }
    

    Here I assume that foam_vec.begin() and foam_vec.end() return iterators over your field. I don't know enough about OpenFOAM to know whether this is the case, but I guess you do :-)
    If foam_vec returns actual const-pointers, you can use the faster viennacl::fast_copy instead of viennacl::copy.

     
  • tario

    tario - 2017-08-31

    Thank you very much!

     

Log in to post a comment.