How to compile thrust and c++ project? - c++

I got big problems about the compile when I want to use thrust and c++ meanwhile.
Here is the project structure(just a test project):
sortbase.h
#include<iostream>
#include <thrust/device_vector.h>
using namespace std;
template<class T>
class SortBase
{
public:
void Init()
{
}
void resize(const int &x)
{
CV.resize(x);
cout<<"resize succeed!"<<endl;
}
private:
thrust::device_vector<T> CV;
};
sorter.h
#ifndef __SORTER_H__
#define __SORTER_H__
#include <iostream>
#include <thrust/device_vector.h>
#include "sortbase.h"
using namespace std;
template<class T>
void ttest()
{cout<<"tttttttttttttttt";cout<<endl;}
template<class KEY>
class Sorter
{
public:
Sorter():CV(NULL)
{
CV=new SortBase<KEY>();
}
~Sorter()
{
if (CV!=NULL)
{
delete CV;
CV=NULL;
}
}
__host__ __device__
virtual bool operator()(const KEY& x,const KEY& y)const
{
return (x<y);
}
void test()
{
//ttest<KEY>();
resize(5);
}
private:
void resize(const int &x)
{
CV->resize(x);
cout<<"resize: "<<x<<endl;
}
private:
SortBase<KEY> *CV;
};
#endif
main.cpp
#include <iostream>
#include "include/sorter.cu"
using namespace std;
int main(int argc,char **argv)
{
Sorter<int> *sorter=new Sorter<int>();
sorter->test();
return 0;
}
What I do like this:
nvcc -c sorter.cu
g++ -c main.cpp -I/usr/local/cuda/include
g++ sorter.o main.o -o sorter
but the second step gives the error message, a lot of warning and error message.
who can help me?
some warning and error message like these:
/usr/local/cuda/include/thrust/detail/device/cuda/for_each.inl: In function \u2018RandomAccessIterator thrust::detail::device::cuda::for_each_n(RandomAccessIterator, Size, UnaryFunction) [with RandomAccessIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, UnaryFunction = thrust::detail::generate_functor<thrust::detail::fill_functor<int> >]\u2019:
/usr/local/cuda/include/thrust/detail/device/dispatch/for_each.h:56: instantiated from \u2018RandomAccessIterator thrust::detail::device::dispatch::for_each_n(RandomAccessIterator, Size, UnaryFunction, thrust::detail::cuda_device_space_tag) [with RandomAccessIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, UnaryFunction = thrust::detail::generate_functor<thrust::detail::fill_functor<int> >]\u2019
/usr/local/cuda/include/thrust/detail/device/for_each.inl:43: instantiated from \u2018OutputIterator thrust::detail::device::for_each_n(OutputIterator, Size, UnaryFunction) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, UnaryFunction = thrust::detail::generate_functor<thrust::detail::fill_functor<int> >]\u2019
/usr/local/cuda/include/thrust/detail/dispatch/for_each.h:84: instantiated from \u2018OutputIterator thrust::detail::dispatch::for_each_n(OutputIterator, Size, UnaryFunction, thrust::device_space_tag) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, UnaryFunction = thrust::detail::generate_functor<thrust::detail::fill_functor<int> >]\u2019
/usr/local/cuda/include/thrust/detail/for_each.inl:41: instantiated from \u2018OutputIterator thrust::detail::for_each_n(OutputIterator, Size, UnaryFunction) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, UnaryFunction = thrust::detail::generate_functor<thrust::detail::fill_functor<int> >]\u2019
/usr/local/cuda/include/thrust/detail/generate.inl:47: instantiated from \u2018OutputIterator thrust::generate_n(OutputIterator, Size, Generator) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, Generator = thrust::detail::fill_functor<int>]\u2019
/usr/local/cuda/include/thrust/detail/device/cuda/fill.inl:87: instantiated from \u2018OutputIterator thrust::detail::device::cuda::detail::fill_n(OutputIterator, Size, const T&, thrust::detail::false_type) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, T = int]\u2019
/usr/local/cuda/include/thrust/detail/device/cuda/fill.inl:117: instantiated from \u2018OutputIterator thrust::detail::device::cuda::detail::fill_n(OutputIterator, Size, const T&, thrust::detail::true_type) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, T = int]\u2019
/usr/local/cuda/include/thrust/detail/device/cuda/fill.inl:138: instantiated from \u2018OutputIterator thrust::detail::device::cuda::fill_n(OutputIterator, Size, const T&) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, T = int]\u2019
/usr/local/cuda/include/thrust/detail/device/dispatch/fill.h:57: instantiated from \u2018OutputIterator thrust::detail::device::dispatch::fill_n(OutputIterator, Size, const T&, thrust::detail::cuda_device_space_tag) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, T = int]\u2019
/usr/local/cuda/include/thrust/detail/device/fill.inl:47: instantiated from \u2018OutputIterator thrust::detail::device::fill_n(OutputIterator, Size, const T&) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, T = int]\u2019
/usr/local/cuda/include/thrust/detail/dispatch/fill.h:76: instantiated from \u2018OutputIterator thrust::detail::dispatch::fill_n(OutputIterator, Size, const T&, thrust::device_space_tag) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, T = int]\u2019
/usr/local/cuda/include/thrust/detail/fill.inl:46: instantiated from \u2018OutputIterator thrust::fill_n(OutputIterator, Size, const T&) [with OutputIterator = thrust::detail::normal_iterator<thrust::device_ptr<int> >, Size = long unsigned int, T = int]\u2019
/usr/local/cuda/include/thrust/detail/vector_base.inl:757: instantiated from \u2018void thrust::detail::vector_base<T, Alloc>::fill_insert(typename thrust::detail::contiguous_storage<T, Alloc>::iterator, typename thrust::detail::contiguous_storage<T, Alloc>::size_type, const T&) [with T = int, Alloc = thrust::device_malloc_allocator<int>]\u2019
/usr/local/cuda/include/thrust/detail/vector_base.inl:561: instantiated from \u2018void thrust::detail::vector_base<T, Alloc>::insert(typename thrust::detail::contiguous_storage<T, Alloc>::iterator, typename thrust::detail::contiguous_storage<T, Alloc>::size_type, const T&) [with T = int, Alloc = thrust::device_malloc_allocator<int>]\u2019
/usr/local/cuda/include/thrust/detail/vector_base.inl:223: instantiated from \u2018void thrust::detail::vector_base<T, Alloc>::resize(typename thrust::detail::contiguous_storage<T, Alloc>::size_type, typename thrust::detail::contiguous_storage<T, Alloc>::value_type) [with T = int, Alloc = thrust::device_malloc_allocator<int>]\u2019
include/sortbase.h:14: instantiated from void SortBase<T>::resize(const int&) [with T = int]
include/sorter.cu:40: instantiated from void Sorter<KEY>::resize(const int&) [with KEY = int]
include/sorter.cu:35: instantiated from void Sorter<KEY>::test() [with KEY = int]
main.cpp:9: instantiated from here
/usr/local/cuda/include/thrust/detail/device/cuda/for_each.inl:93: error: invalid application of sizeof to incomplete type thrust::detail::STATIC_ASSERTION_FAILURE<false>
/usr/local/cuda/include/thrust/detail/device/cuda/arch.h: At global scope:
/usr/local/cuda/include/thrust/detail/device/cuda/arch.h:59: warning: inline function size_t thrust::detail::device::cuda::arch::compute_capability() used but never defined

I think that the problem is you are including your .cu file from a .cpp file. Thrust code can only be used from .cu files, which must be compiled by NVCC, not by the host compiler g++. So you need to move all of your Thrust and CUDA device code inside .cu files in functions that can be called from your .cpp files.

Harrism is right. If you are going to compile a file using gpu, you must use .cu for the file and nvcc like:
nvcc -o test test.cu

Related

Fill std::map with std::generate_n

I'd like to fill a std::map using std::generate_n but can't get it to work. What I tried is something along these lines:
unsigned number_of_pairs{5};
std::map<std::string, std::string> my_map;
auto read_pair_from_input = [](){
std::string key;
std::getline(std::cin, key);
std::string value;
std::getline(std::cin, value);
return std::make_pair(key, value);
};
std::generate_n(my_map.begin(), number_of_pairs, read_pair_from_input);
This gives me long errors like:
In file included from /opt/wandbox/gcc- head/include/c++/8.0.0/algorithm:62:0,
from prog.cc:1:
/opt/wandbox/gcc-head/include/c++/8.0.0/bits/stl_algo.h: In instantiation of '_OIter std::generate_n(_OIter, _Size, _Generator) [with _OIter = std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >; _Size = unsigned int; _Generator = main()::<lambda()>]':
prog.cc:18:74: required from here
/opt/wandbox/gcc-head/include/c++/8.0.0/bits/stl_algo.h:4468:11: error: use of deleted function 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator= (typename std::conditional<std::__not_<std::__and_<std::is_copy_assignable<_Tp>, std::is_copy_assignable<_T2> > >::value, const std::pair<_T1, _T2>&, const std::__nonesuch_no_braces&>::type) [with _T1 = const std::__cxx11::basic_string<char>; _T2 = std::__cxx11::basic_string<char>; typename std::conditional<std::__not_<std::__and_<std::is_copy_assignable<_Tp>, std::is_copy_assignable<_T2> > >::value, const std::pair<_T1, _T2>&, const std::__nonesuch_no_braces&>::type = const std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >&]'
*__first = __gen();
~~~~~~~~~^~~~~~~~~
In file included from /opt/wandbox/gcc-head/include/c++/8.0.0/utility:70:0,
from /opt/wandbox/gcc-head/include/c++/8.0.0/algorithm:60,
from prog.cc:1:
/opt/wandbox/gcc-head/include/c++/8.0.0/bits/stl_pair.h:378:7: note: declared here
operator=(typename conditional<
^~~~~~~~
Is it possible to fill a std::map with std::generate_n?
What you want is a std::inserter:
std::generate_n(std::inserter(my_map, my_map.begin()), number_of_pairs, read_pair_from_input);
The inserter will wrap your map into an iterator-like construct that std::generate_n can use
Demo
std::generate_n can be implemented like
template< class OutputIt, class Size, class Generator >
OutputIt generate_n( OutputIt first, Size count, Generator g )
{
for( Size i = 0; i < count; i++ ) {
*first++ = g();
}
return first;
}
As you can see it tries to assign the result of the generator to the iterator. This does not work with associative containers as you cannot modify the key as that would break the structure of the container.
What you need is different type of iterator, namely a std::insert_iterator that you can get using std::inserter like
std::generate_n(std::inserter(my_map, my_map.begin()), number_of_pairs, read_pair_from_input);

Error with constructor of private struct inside a class

I have this class:
template <typename C, typename R, typename D>
class Cache {
typedef std::shared_ptr<cc::Distance<C,D>> DistancePtr;
public:
Cache(const DistancePtr distance, const std::function<R(C)> &backEnd, const size_t size = 10000, const float treshold = 0);
...
private:
struct CacheElem{
CacheElem(const C code, const R result, std::list<size_t>::iterator listElem) : code(code), result(result), listElem(listElem) {}
C code;
R result;
std::list<size_t>::iterator listElem; //pointing to corresponding element in lru0
};
...
I instantiated this object with cc::Cache<int,int,int> cache(...) (I don't know if you need to know all passed arguments, let me know in that case), but I get this error that I don't understand at all:
In file included from /usr/include/c++/5/memory:64:0,
from ../main.cpp:9:
/usr/include/c++/5/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = cc::Cache<int, int, int>::CacheElem; _Args = {}]’:
/usr/include/c++/5/bits/stl_uninitialized.h:519:18: required from ‘static _ForwardIterator std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = cc::Cache<int, int, int>::CacheElem*; _Size = long unsigned int; bool _TrivialValueType = false]’
/usr/include/c++/5/bits/stl_uninitialized.h:575:20: required from ‘_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = cc::Cache<int, int, int>::CacheElem*; _Size = long unsigned int]’
/usr/include/c++/5/bits/stl_uninitialized.h:637:44: required from ‘_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = cc::Cache<int, int, int>::CacheElem*; _Size = long unsigned int; _Tp = cc::Cache<int, int, int>::CacheElem]’
/usr/include/c++/5/bits/stl_vector.h:1311:36: required from ‘void std::vector<_Tp, _Alloc>::_M_default_initialize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = cc::Cache<int, int, int>::CacheElem; _Alloc = std::allocator<cc::Cache<int, int, int>::CacheElem>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/5/bits/stl_vector.h:279:30: required from ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = cc::Cache<int, int, int>::CacheElem; _Alloc = std::allocator<cc::Cache<int, int, int>::CacheElem>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<cc::Cache<int, int, int>::CacheElem>]’
/home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Core/Cache.hpp:59:85: required from ‘cc::Cache<C, R, D>::Cache(cc::Cache<C, R, D>::DistancePtr, const std::function<R(C)>&, size_t, float) [with C = int; R = int; D = int; cc::Cache<C, R, D>::DistancePtr = std::shared_ptr<cc::Distance<int, int> >; size_t = long unsigned int]’
../main.cpp:33:53: required from here
/usr/include/c++/5/bits/stl_construct.h:75:7: error: no matching function for call to ‘cc::Cache<int, int, int>::CacheElem::CacheElem()’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^
In file included from ../Core/CCCore.hpp:19:0,
from ../main.cpp:15:
/home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Core/Cache.hpp:36:4: note: candidate: cc::Cache<C, R, D>::CacheElem::CacheElem(C, R, std::__cxx11::list<long unsigned int>::iterator) [with C = int; R = int; D = int; std::__cxx11::list<long unsigned int>::iterator = std::_List_iterator<long unsigned int>]
CacheElem(const C code, const R result, std::list<size_t>::iterator listElem) : code(code), result(result), listElem(listElem) {}
^
/home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Core/Cache.hpp:36:4: note: candidate expects 3 arguments, 0 provided
/home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Core/Cache.hpp:35:10: note: candidate: constexpr cc::Cache<int, int, int>::CacheElem::CacheElem(const cc::Cache<int, int, int>::CacheElem&)
struct CacheElem{
^
/home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Core/Cache.hpp:35:10: note: candidate expects 1 argument, 0 provided
/home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Core/Cache.hpp:35:10: note: candidate: constexpr cc::Cache<int, int, int>::CacheElem::CacheElem(cc::Cache<int, int, int>::CacheElem&&)
/home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Core/Cache.hpp:35:10: note: candidate expects 1 argument, 0 provided
<builtin>: recipe for target 'main.o' failed
make: *** [main.o] Error 1
Why this happen?
UPDATE TO COMMENT ONE ANSWER:
Ok I found out where the problem is, but not why it occours. The Cache constructor implementation is this one:
template <typename C, typename R, typename D>
Cache<C,R,D>::Cache(const DistancePtr distance, const std::function<R(C)> &backEnd, const size_t size, const float treshold)
: distance(distance), backEnd(backEnd), values(size), treshold(treshold), size(size) {}
Where values is declared as:
std::vector<CacheElem> values;
But if I delete values(size) from the constructor above, everything works fine and no compile error occurs. Why? And hot to solve it?
It looks like the problem is that you're missing a default constructor for CacheElem. Somewhere in your code, a CacheElem needs to be default constructed, either because you stored it in an STL container that requires elements to be default constructible, or because you simply tried to instantiate one somewhere without calling a constructor.
I found the error thanks to the tip from this answer.
Changing the Cache constructor implementation from this:
template <typename C, typename R, typename D>
Cache<C,R,D>::Cache(const DistancePtr distance, const std::function<R(C)> &backEnd, const size_t size, const float treshold)
: distance(distance), backEnd(backEnd), values(size), treshold(treshold), size(size) {}
To this:
template <typename C, typename R, typename D>
Cache<C,R,D>::Cache(const DistancePtr distance, const std::function<R(C)> &backEnd, const size_t size, const float treshold)
: distance(distance), backEnd(backEnd), treshold(treshold), size(size) {
values.reserve(size);
}
Solved the problem. I think because calling values(size) is implicitly calling resize() which is illegal without providing CacheElem constructor elements.

BGL dfs from a set of source nodes

Problem
Having an adjacency list graph, I would like to traverse it with a DFS algorithm from a specific set of source nodes. The main problem is that the color map is passed by value.
I tried
To encapsulate the color map by reference into a structure :
class ref_color_map_wrapper
{
public:
typedef boost::default_color_type color_type;
typedef std::vector<color_type> color_map_type;
private:
color_map_type &color_map;
public:
ref_color_map_wrapper(color_map_type& color_map)
: color_map(color_map)
{}
color_type& operator[](size_t i)
{
return color_map[i];
}
const color_type& operator[](size_t i) const
{
return color_map[i];
}
};
namespace boost
{
template <>
struct property_traits<ref_color_map_wrapper>
{
typedef boost::read_write_property_map_tag category;
typedef boost::default_color_type value_type;
typedef boost::default_color_type& reference;
typedef size_t key_type;
};
void put(ref_color_map_wrapper& color_map, vertex_descriptor& v, boost::default_color_type color)
{
color_map[v] = color;
}
boost::default_color_type get(ref_color_map_wrapper& color_map, vertex_descriptor& v)
{
return color_map[v];
}
void put(ref_color_map_wrapper& color_map, const vertex_descriptor& v, boost::default_color_type color)
{
color_map[v] = color;
}
boost::default_color_type get(const ref_color_map_wrapper& color_map, const vertex_descriptor& v)
{
return color_map[v];
}
}
And finally the code of the DFS :
typedef std::vector<boost::default_color_type> color_map_type;
color_map_type color_map(boost::num_vertices(graph), boost::white_color);
ref_color_map_wrapper ref_color_map(color_map);
for(auto it = root_set.begin(); it != root_set.end(); ++it)
{
size_t i = boost::get(boost::vertex_index_t(), graph, *it);
if(color_map[i] == boost::white_color)
{
boost::depth_first_visit(graph, *it, boost::default_dfs_visitor(), ref_color_map);
}
}
Compilation error
/usr/local/include/boost/property_map/property_map.hpp: In instantiation of ‘void boost::ReadablePropertyMapConcept<PMap, Key>::constraints() [with PMap = gc::ref_color_map_wrapper; Key = long unsigned int]’:
/usr/local/include/boost/concept/detail/has_constraints.hpp:32:14: required by substitution of ‘template<class Model> boost::concepts::detail::yes boost::concepts::detail::has_constraints_(Model*, boost::concepts::detail::wrap_constraints<Model, (& Model:: constraints)>*) [with Model = boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>]’
/usr/local/include/boost/concept/detail/has_constraints.hpp:42:5: required from ‘const bool boost::concepts::not_satisfied<boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >::value’
/usr/local/include/boost/concept/detail/has_constraints.hpp:45:31: required from ‘struct boost::concepts::not_satisfied<boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >’
/usr/local/include/boost/mpl/if.hpp:67:11: required from ‘struct boost::mpl::if_<boost::concepts::not_satisfied<boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >, boost::concepts::constraint<boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >, boost::concepts::requirement<boost::concepts::failed************ boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>::************> >’
/usr/local/include/boost/concept/detail/general.hpp:50:8: required from ‘struct boost::concepts::requirement_<void (*)(boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>)>’
/usr/local/include/boost/concept_check.hpp:45:1: [ skipping 4 instantiation contexts ]
/usr/local/include/boost/concept/detail/has_constraints.hpp:45:31: required from ‘struct boost::concepts::not_satisfied<boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >’
/usr/local/include/boost/mpl/if.hpp:67:11: required from ‘struct boost::mpl::if_<boost::concepts::not_satisfied<boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >, boost::concepts::constraint<boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >, boost::concepts::requirement<boost::concepts::failed************ boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>::************> >’
/usr/local/include/boost/concept/detail/general.hpp:50:8: required from ‘struct boost::concepts::requirement_<void (*)(boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>)>’
/usr/local/include/boost/graph/depth_first_search.hpp:88:1: required from ‘void boost::detail::depth_first_visit_impl(const IncidenceGraph&, typename boost::graph_traits<Graph>::vertex_descriptor, DFSVisitor&, ColorMap, TerminatorFunc) [with IncidenceGraph = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, boost::variant<const void*, std::pair<void*, void*> > >; DFSVisitor = boost::dfs_visitor<>; ColorMap = gc::ref_color_map_wrapper; TerminatorFunc = boost::detail::nontruth2; typename boost::graph_traits<Graph>::vertex_descriptor = long unsigned int]’
/usr/local/include/boost/graph/depth_first_search.hpp:314:5: required from ‘void boost::depth_first_visit(const IncidenceGraph&, typename boost::graph_traits<Graph>::vertex_descriptor, DFSVisitor, ColorMap) [with IncidenceGraph = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, boost::variant<const void*, std::pair<void*, void*> > >; DFSVisitor = boost::dfs_visitor<>; ColorMap = gc::ref_color_map_wrapper; typename boost::graph_traits<Graph>::vertex_descriptor = long unsigned int]’
../include/garbage_collector.hpp:169:87: required from here
/usr/local/include/boost/property_map/property_map.hpp:200:7: error: no matching function for call to ‘get(gc::ref_color_map_wrapper&, long unsigned int&)’
/usr/local/include/boost/property_map/property_map.hpp:200:7: note: candidates are:
In file included from /usr/local/include/boost/tuple/tuple.hpp:33:0,
from /usr/local/include/boost/unordered/detail/allocate.hpp:27,
from /usr/local/include/boost/unordered/detail/buckets.hpp:15,
from /usr/local/include/boost/unordered/detail/table.hpp:10,
from /usr/local/include/boost/unordered/detail/equivalent.hpp:14,
from /usr/local/include/boost/unordered/unordered_set.hpp:17,
from /usr/local/include/boost/unordered_set.hpp:16,
from /usr/local/include/boost/graph/adjacency_list.hpp:21,
from ../include/garbage_collector.hpp:6,
from main.cpp:3:
/usr/local/include/boost/tuple/detail/tuple_basic.hpp:225:1: note: template<int N, class HT, class TT> typename boost::tuples::access_traits<typename boost::tuples::element<N, boost::tuples::cons<HT, TT> >::type>::const_type boost::tuples::get(const boost::tuples::cons<HT, TT>&)
/usr/local/include/boost/tuple/detail/tuple_basic.hpp:225:1: note: template argument deduction/substitution failed:
In file included from /usr/local/include/boost/graph/adjacency_list.hpp:36:0,
from ../include/garbage_collector.hpp:6,
from main.cpp:3:
/usr/local/include/boost/property_map/property_map.hpp:200:7: note: ‘gc::ref_color_map_wrapper’ is not derived from ‘const boost::tuples::cons<HT, TT>’
In file included from /usr/local/include/boost/tuple/tuple.hpp:33:0,
from /usr/local/include/boost/unordered/detail/allocate.hpp:27,
from /usr/local/include/boost/unordered/detail/buckets.hpp:15,
from /usr/local/include/boost/unordered/detail/table.hpp:10,
from /usr/local/include/boost/unordered/detail/equivalent.hpp:14,
from /usr/local/include/boost/unordered/unordered_set.hpp:17,
from /usr/local/include/boost/unordered_set.hpp:16,
from /usr/local/include/boost/graph/adjacency_list.hpp:21,
from ../include/garbage_collector.hpp:6,
from main.cpp:3:
/usr/local/include/boost/tuple/detail/tuple_basic.hpp:211:1: note: template<int N, class HT, class TT> typename boost::tuples::access_traits<typename boost::tuples::element<N, boost::tuples::cons<HT, TT> >::type>::non_const_type boost::tuples::get(boost::tuples::cons<HT, TT>&)
/usr/local/include/boost/tuple/detail/tuple_basic.hpp:211:1: note: template argument deduction/substitution failed:
In file included from /usr/local/include/boost/graph/adjacency_list.hpp:36:0,
from ../include/garbage_collector.hpp:6,
from main.cpp:3:
/usr/local/include/boost/property_map/property_map.hpp:200:7: note: ‘gc::ref_color_map_wrapper’ is not derived from ‘boost::tuples::cons<HT, TT>’
/usr/local/include/boost/property_map/property_map.hpp:179:19: note: template<class T> const T& get(const T*, std::ptrdiff_t)
/usr/local/include/boost/property_map/property_map.hpp:179:19: note: template argument deduction/substitution failed:
/usr/local/include/boost/property_map/property_map.hpp:200:7: note: mismatched types ‘const T*’ and ‘gc::ref_color_map_wrapper’
Graph definition
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, vertex_info_type> graph_type;
What's working
boost::get(ref_color_map, *it);
boost::put(ref_color_map, *it, boost::white_color);
works without any compilation error…
You need to put your get() and put() functions in the namespace where ref_color_map resides, since they are found via ADL. See here.

C++ how to insert array into hash set?

I need to insert a 1D array into the hashset.
But I got error while compiling.
#include <stdio.h>
#include <stdlib.h>
#include <hash_set.h>
using namespace std;
int hash_comp(const int* state1,const int* state2) {
int result = 0;
for (i = 0; i < 16; i++)
{
if (state1[i] != state2[i]) {
result = -1;
}
}
return result;
}
struct eqArray
{
bool operator()(const int* a1,const int* a2) const
{
return hash_comp(a1,a2) == 0;
}
};
hash_set<int*,hash<int*>,eqArray> closelist;
int main(int argc, char** argv)
{
const int sn[16] = {1,2,3,4,5,6,0,8,9,10,11,12,13,14,7,15};
closelist.insert(sn);
return 0;
}
/usr/include/c++/4.2.1/ext/hashtable.h: In member function 'size_t __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::_M_bkt_num_key(const _Key&, size_t) const [with _Val = int*, _Key = int*, _HashFcn = __gnu_cxx::hash<int*>, _ExtractKey = std::_Identity<int*>, _EqualKey = std::equal_to<int*>, _Alloc = std::allocator<int*>]':
/usr/include/c++/4.2.1/ext/hashtable.h:599: instantiated from 'size_t __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::_M_bkt_num(const _Val&, size_t) const [with _Val = int*, _Key = int*, _HashFcn = __gnu_cxx::hash<int*>, _ExtractKey = std::_Identity<int*>, _EqualKey = std::equal_to<int*>, _Alloc = std::allocator<int*>]'
/usr/include/c++/4.2.1/ext/hashtable.h:1006: instantiated from 'void __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::resize(size_t) [with _Val = int*, _Key = int*, _HashFcn = __gnu_cxx::hash<int*>, _ExtractKey = std::_Identity<int*>, _EqualKey = std::equal_to<int*>, _Alloc = std::allocator<int*>]'
/usr/include/c++/4.2.1/ext/hashtable.h:437: instantiated from 'std::pair<__gnu_cxx::_Hashtable_iterator<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>, bool> __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::insert_unique(const _Val&) [with _Val = int*, _Key = int*, _HashFcn = __gnu_cxx::hash<int*>, _ExtractKey = std::_Identity<int*>, _EqualKey = std::equal_to<int*>, _Alloc = std::allocator<int*>]'
/usr/include/c++/4.2.1/ext/hash_set:197: instantiated from 'std::pair<typename __gnu_cxx::hashtable<_Value, _Value, _HashFcn, std::_Identity<_Tp>, _EqualKey, _Alloc>::const_iterator, bool> __gnu_cxx::hash_set<_Value, _HashFcn, _EqualKey, _Alloc>::insert(const typename __gnu_cxx::hashtable<_Value, _Value, _HashFcn, std::_Identity<_Tp>, _EqualKey, _Alloc>::value_type&) [with _Value = int*, _HashFcn = __gnu_cxx::hash<int*>, _EqualKey = std::equal_to<int*>, _Alloc = std::allocator<int*>]'
src/ods2.cpp:677: instantiated from here
If you use a std::array<int, 16> instead of int*, all your problems will go away. If you have no C++11 compiler, you can use boost::array instead. Also, replace hash_set with unordered_set.
It appears there is no specialization of std::hash for std::arrays, so I wrote my own:
#include <unordered_set>
#include <array>
namespace std
{
template<typename T, size_t N>
struct hash<array<T, N> >
{
typedef array<T, N> argument_type;
typedef size_t result_type;
result_type operator()(const argument_type& a) const
{
hash<T> hasher;
result_type h = 0;
for (result_type i = 0; i < N; ++i)
{
h = h * 31 + hasher(a[i]);
}
return h;
}
};
}
std::unordered_set<std::array<int, 16> > closelist;
int main()
{
std::array<int, 16> sn = {1,2,3,4,5,6,0,8,9,10,11,12,13,14,7,15};
closelist.insert(sn);
}
You didn’t post the actual error message, only the trace. That said, it’s probably because your set uses non-const int* while your data is const.
That said, I’d suggest using unordered_set instead of hash_set(either from std::tr1 if your compiler supports that, or from std if your compiler supports C++11 or from Boost), and heed Fred’s advice of using an {std,boost}::array instead of a raw pointer.
I don't think that there exists a specialized hash<int*>. I added this:
namespace __gnu_cxx{ //I'm not sure what compiler version you used,
//mine wanted this
template<>
struct hash<const int*>{
size_t operator()(const int*a) const{
size_t r = 0;
for (int i=0;i<16;i++)
r = (r<<1) ^ a[i]; //not sure if it makes sense as a hash.
return r;
}
};
}
I also put in some consts and it compiles.

Accessing a subslice of a boost matrix in a helper class

I'm trying to build a class that eases the use of the boost boost::numeric::ublas::matrix. Thus I've got:
using namespace boost::numeric::ublas;
typedef matrix<double> matrix_t;
class Tensor : public matrix_t {
public:
Tensor (const int M, const int N) : matrix_t(M, N) { }
virtual ~Tensor() { }
Tensor SubMatrix (const int start1, const int size1, const int start2, const int size2) const;
void Print() const;
}; // tensor
And I further define the SubMatrix() as follows:
Tensor Tensor::SubMatrix (const int start1, const int size1, const int start2, const int size2) const {
matrix_slice<matrix_t> s (this, slice(start1, 1, size1), slice(start2, 1, size2));
Tensor t (matrix_expression<matrix_t> (s));
return t;
}
I'd like to be able to easily create new Tensors by grabbing sub-matrices from within a Tensor. The compiler is complaining with the following:
g++ -ftemplate-depth-100 -Drestrict= -Wall -Wno-deprecated -g3 -ggdb -Wall -D_GLIBCXX_DEBUG -I/home/eshamay/md/src -I/home/eshamay/share/include -I/usr/include -I/usr/local/include -I/home/eshamay/src/boost-1_43_0 -L/home/eshamay/share/lib -L/home/eshamay/src/lapack-3.2.1 -lconfig++ -c -o morita2002.o morita2002.cpp
morita2002.cpp: In member function ‘morita::math::Tensor morita::math::Tensor::SubMatrix(int, int, int, int) const’:
morita2002.cpp:109: error: no matching function for call to ‘boost::numeric::ublas::matrix_slice<boost::numeric::ublas::matrix<double, boost::numeric::ublas:: basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > > >::matrix_slice(const morita::math::Tensor* const, boost::numeric::ublas::slice, boost::numeric::ublas::slice)’
/usr/include/boost/numeric/ublas/matrix_proxy.hpp:3192: note: candidates are: boost::numeric::ublas::matrix_slice<E>::matrix_slice(const typename boost::mpl:: if_<boost::is_const<T>, typename M::const_closure_type, typename M::closure_type>::type&, const boost::numeric::ublas::basic_slice<typename A::size_type, typename A:: difference_type>&, const boost::numeric::ublas::basic_slice<typename A::size_type, typename A::difference_type>&, int) [with M = boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >]
/usr/include/boost/numeric/ublas/matrix_proxy.hpp:3183: note: boost::numeric::ublas::matrix_slice<E>::matrix_slice(M&, const boost::numeric::ublas:: basic_slice<typename A::size_type, typename A::difference_type>&, const boost::numeric::ublas::basic_slice<typename A::size_type, typename A::difference_type>&) [with M = boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std:: allocator<double> > >]
/usr/include/boost/numeric/ublas/matrix_proxy.hpp:3155: note: boost::numeric::ublas::matrix_slice<boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > > >::matrix_slice(const boost::numeric::ublas::matrix_slice<boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > > >&)
morita2002.cpp:112: error: conversion from ‘morita::math::Tensor (*)(boost::numeric::ublas::matrix_expression<boost::numeric::ublas::matrix<double, boost::numeric:: ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > > >)’ to non-scalar type ‘morita::math:: Tensor’ requested
After attempting a number of variations to appease the compiler, I'm stumped. What's the issue, and why can't I just create the matrix_slice the way it's written?
matrix_slice requires a reference as its first argument, while you're giving it a pointer (this). Try using *this, instead.
Furthermore, since you're SubMatrix method has a const qualifier, the type of *this would be matrix_t const&, so you should use matrix_slice<matrix_t const>.