FAQ  •  Login

HW9 count_if problems

<<

f13-759-rmays

Newbie
Newbie

Posts: 49

Joined: Mon Sep 09, 2013 9:12 am

Unread post Sun Nov 17, 2013 1:44 pm

HW9 count_if problems

Hi everyone,

Has anyone had success using the thrust::count_if() function? I'm trying to do this:

  Code:
 struct isGreaterThan5{
    //returns whether an input x is greater than 5
    __host__ __device__ bool operator()(int &x){
      return x > 5;
    }
  };

  int numGreaterThanFive = thrust::count_if(rawMeasurements,rawMeasurements+N, isGreaterThan5());


When I try to compile it I get this error:

  Code:
error: no instance of overloaded function "thrust::count_if" matches the argument list
            argument types are: (int [15], int *, isGreaterThan5)


Any idea what I'm doing wrong? I've even tried the code in the thrust documentation and get the same result: http://docs.thrust.googlecode.com/hg/gr ... nting.html
<<

Dan Negrut

Global Moderator
Global Moderator

Posts: 833

Joined: Wed Sep 03, 2008 12:24 pm

Unread post Sun Nov 17, 2013 4:45 pm

Re: HW9 count_if problems

I'll ask Andrew to see if there is some issue with the latest CUDA and the version of thrust we've got after the update to 6.0.
Stay tuned,
Dan
<<

S12ali

Newbie
Newbie

Posts: 38

Joined: Mon Jan 23, 2012 9:07 pm

Unread post Sun Nov 17, 2013 4:54 pm

Re: HW9 count_if problems

Hi Owen,

Try this:

`` __host__ __device__ bool operator()(const int &x)''

and other part of code remains the same.

Though I'm not totally sure why this will make a difference, when we're writing functors, generally speaking the parameter should not be a non-constant reference. It can be a const reference, or simply passing by value (i.e. ``int x'').
<<

Andrew Seidl

Administrator
Administrator

Posts: 193

Joined: Thu Oct 28, 2010 11:54 am

Unread post Sun Nov 17, 2013 6:21 pm

Re: HW9 count_if problems

(Looks like Ang beat me to it.)

I think that example might not be correct, or at least outdated. I usually just grep/ag the actual code examples for correct usage: https://github.com/thrust/thrust/tree/master/examples Also, I'm not sure if the docs on Google Code are being kept up to date after the migration to Github. Latest docs are at: http://thrust.github.io/doc/group__counting.html (but still looks like it hasn't been updated).


Dan: 6.0 should be publicly available early next year. Only a couple of us have access before then.
<<

f13-759-psankaranara

Newbie
Newbie

Posts: 17

Joined: Mon Sep 09, 2013 9:12 am

Unread post Sun Nov 17, 2013 9:15 pm

Re: HW9 count_if problems

Hi Andrew and Ang Li,

I have tried passing by reference and by value, but I still get the same error as Owen.

@Owen: Did the changes solve the problem for you?
-Praveen
<<

f13-759-rmays

Newbie
Newbie

Posts: 49

Joined: Mon Sep 09, 2013 9:12 am

Unread post Mon Nov 18, 2013 10:56 am

Re: HW9 count_if problems

Andrew, thanks for the tip about the examples. After cloning that repository and running grep for count_if I found an example in "stream_compaction.cu"

Here's the code that works for me.
Outside of main, declare the test function:
  Code:
template <typename T> struct isGreaterThan5 : public thrust::unary_function<T,bool>{
  //returns whether an input x is greater than 5
  __host__ __device__ bool operator()(T x){
    return x > 5;
  }
};


Then inside your main function you can count the number of elements in rawMeasurements that are greater than five:

  Code:
 int numGreaterThanFive = thrust::count_if(rawMeasurements,rawMeasurements+N, isGreaterThan5<int>());


-Owen
<<

f13-759-odeshmukh

Newbie
Newbie

Posts: 22

Joined: Mon Sep 09, 2013 9:12 am

Unread post Mon Nov 18, 2013 12:12 pm

Re: HW9 count_if problems

It works even if I don't use templates, which fits the requirements for this homework. I have the struct defined like:

  Code:
 struct count5{
   __host__ __device__
    bool operator()(int x) {
    //magic
    }
  };


And one can directly pass this struct to count_if, no need to create another instance of that type and then pass it.
Thanks,
Omkar
<<

f13-759-rmays

Newbie
Newbie

Posts: 49

Joined: Mon Sep 09, 2013 9:12 am

Unread post Mon Nov 18, 2013 4:08 pm

Re: HW9 count_if problems

Omkar, it looks like that only works if you use Compute 1.0. I was compiling with --arch compute_20 which apparently requires the template-based code.

-Owen
<<

f13-759-odeshmukh

Newbie
Newbie

Posts: 22

Joined: Mon Sep 09, 2013 9:12 am

Unread post Mon Nov 18, 2013 8:50 pm

Re: HW9 count_if problems

I checked by passing flags "-arch=sm_20" and "-arch=compute_20" and seems to be working for me.
Thanks,
Omkar

Return to ME759 Fall 2013: High Performance Computing

Who is online

Users browsing this forum: No registered users and 3 guests

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software.