How is OpenCL floatn different from a custom struct - struct

I'm having problems passing a structure of vectors to an OpenCL kernel.
Long story short, I have found that I CAN pass a C structure:
typedef struct {
cl_float4 vec;
} my_type;
. . . to a kernel, if I declare it like:
typedef struct { float s[4]; } my_float4;
typedef struct { my_float4 vec; } my_type; //use custom float4
__kernel void function(const my_type test) {}
. . . but NOT, if I declare it like:
typedef struct { float4 vec; } my_type; //use built-in float4
__kernel void function(const my_type test) {}
So my question is, what makes a float4 different and special? In the second example, I get CL_INVALID_ARG_SIZE on clSetKernelArg. In the host code, I'm using cl_float4--isn't that supposed to be paired with float4 in the kernel?
EDIT: by request, the clSetKernelArg code (error handling cropped):
template <typename type_data> void set_argument(int arg_index, const type_data* data) {
cl_int err = clSetKernelArg(kernel,arg_index,sizeof(type_data),data);
if (err!=CL_SUCCESS) throw "ERROR"; /*simplified*/
}
I call the function with a struct declared exactly as above:
my_type test;
my_kernel->set_argument(0,&test);

This sounds like a bug with your implementation - you should be able to pass that user-defined type to a kernel without issue. What platform / SDK are you using? I was able to successfully declare this type in host code:
typedef struct _mytype {
cl_float4 val;
} mytype;
... and then later use it:
mytype t;
status = clSetKernelArg(kernel, argc++, sizeof(mytype), &t);
check_status("clSetKernelArg:mytype t", status);
Where check_status does what you'd expect: checks for a non-CL_SUCCESS return value.
The kernel looks like this:
typedef struct _mytype {
float4 value;
} mytype;
kernel void do_it(/* ... */, const mytype test) { /* ... */ }
This compiles and executes, giving correct results on the CPU device.

Related

Auto-interpreting a c-style string as a std::string via Boost's Property tree's .get function

I use boosts property tree, included via
#include "boost\property_tree\ptree.hpp"
And... I'd like to create a simple function which substitutes a value in case none is found via a fairly straight-forward template function:
template <typename Type>
Type getValueOrDefault( std::string const& str, Type defaultValue )
{
Type returnValue = defaultValue;
try {
returnValue = mSettings.get<Type>( str );
}
catch ( boost::property_tree::ptree_error &e )
{
// Log error!
}
return returnValue;
}
This works well in principle, but runs into a bit problems if I rely on C-style string. For example, calling the function as follows:
getValueOrDefault( "pathToImportantStuffParameter", "c:/defaultdir/" )
will result in the following error:
boost\property_tree\stream_translator.hpp(36): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::basic_istream<char,std::char_traits<char>>' (or there is no acceptable conversion)
The error stems from passing char const * as a template parameter which makes a fair bit of sense. Two obvious solutions to this issue would be to force the default value to be a std::string object, like so:
getValueOrDefault<std::string>( "pathToImportantStuffParameter", "c:/defaultdir/" )
getValueOrDefault( "pathToImportantStuffParameter", std::string("c:/defaultdir/") )
But I'm wondering if someone might know of some template magic I could sprinkle to automatically interpret c-style strings as std::strings?
You can provide a char array overload which converts the char array to a std::string and then calls the default implementation:
#include <iostream>
#include <string>
template <typename T>
T getValueOrDefault(const std::string& str, T&& defaultValue)
{
std::cout << "inside default implementation" << std::endl;
/* ... */
return defaultValue;
}
template <std::size_t N>
std::string getValueOrDefault(const std::string& str, const char (&defaultValue)[N])
{
std::cout << "inside char[] overload" << std::endl;
return getValueOrDefault(str, std::string(defaultValue));
}
int main()
{
auto x = getValueOrDefault("foo", "bar");
return 0;
}
live example
An alternative solution is to use custom type traits:
#include <string>
#include <type_traits>
template <typename T>
struct return_type
{
using type = T;
};
template <>
struct return_type<const char*>
{
using type = std::string;
};
template <typename T>
using return_type_t = typename return_type<typename std::decay<T>::type>::type;
template <typename T>
return_type_t<T> getValueOrDefault(const std::string& str, T&& defaultValue)
{
return_type_t<T> value(defaultValue);
/* ... */
return value;
}
int main()
{
auto x = getValueOrDefault("foo", "bar");
static_assert(std::is_same<decltype(x), std::string>::value, "");
return 0;
}
live example
The only way I found is to specialize getValueOrDefault for const char*, which calls getValueOrDefault with std::string explicitly:
//Note that the return value is unspecified, it returns a 'const char*' to a temporary,
//which will be destroyed when the function returns
template <>
const char* getValueOrDefault(std::string const& str, const char* defaultValue)
{
return getValueOrDefault<std::string>(str, defaultValue).c_str();
}
If you want to that function to return a std::string instead of an invalid const char*, you have to change the template signature a bit:
//Default return type is the same as paramter
template <typename Type, typename Return = Type>
Return getValueOrDefault(std::string const& str, Type defaultValue)
{
//...
}
//Trick the compiler to select this overload for 'const char*'
template <typename Return = std::string>
Return getValueOrDefault(std::string const& str, const char* defaultValue)
{
return getValueOrDefault<std::string, std::string>(str, defaultValue);
}
or you could just plain overload the function (thanks #m.s.)
//Overload for 'const char*'
std::string getValueOrDefault(std::string const& str, const char* defaultValue)
{
return getValueOrDefault<std::string>(str, defaultValue);
}
There is also a third way (if you can use C++14), using the string literal ""s:
//"c:/defaultdir/"s is a std::string (note the s after it => string literal)
getValueOrDefault("pathToImportantStuffParameter", "c:/defaultdir/"s);

Use structure member within s a structure using typedef

When I compile the following I get use of undeclared identifier 'rsdtHeader'
How can I do the following operation using typedef?
typedef struct
{
int length;
int x;
int y;
} SdtHeader_s;
typedef struct
{
SdtHeader_s rsdtHeader;
SdtHeader_s* rsdtEntry[(rsdtHeader.length - sizeof(rsdtHeader))/4];
} Rsdt_s;

Generating unique IDs for types at compile time

I want to generate a unique id for every type at compile time. Is this possible in Rust?
So far, I have the following code
//Pseudo code
struct ClassTypeId{
id: &'static uint
}
impl ClassTypeId{
fn get_type<T>(&mut self) -> &'static uint {
let _id :&'static uint = self.id + 1;
self.id = _id;
_id
}
}
let c = ClassTypeId{id:0};
c.get_type::<i32>(); // returns 1
c.get_type::<f32>(); // returns 2
c.get_type::<i32>(); // returns 1
c.get_type::<uint>(); // returns 3
I stole this idea from a C++ library, which looks like this
typedef std::size_t TypeId;
template <typename TBase>
class ClassTypeId
{
public:
template <typename T>
static TypeId GetTypeId()
{
static const TypeId id = m_nextTypeId++;
return id;
}
private:
static TypeId m_nextTypeId;
};
template <typename TBase>
TypeId ClassTypeId<TBase>::m_nextTypeId = 0;
}
std::any::TypeId does something like that:
use std::any::TypeId;
fn main() {
let type_id = TypeId::of::<isize>();
println!("{:?}", type_id);
}
outputs:
TypeId { t: 4150853580804116396 }
This sounds like a job for the bitflags! macro:
#[macro_use] extern crate rustc_bitflags;
bitflags!(
#[derive(Debug)]
flags ComponentMask: u8 {
const Render = 0b00000001,
const Position = 0b00000010,
const Physics = 0b00000100
}
);
// the set of components owned by an entity:
let owned_components: = Render | Position;
// check whether an entity has a certain component:
if owned_components.contains(Physics) { ... }
http://doc.rust-lang.org/rustc_bitflags/macro.bitflags!.html
If you want to manage type ids manually, you can use my unique-type-id crate. It allows you to specify what ids a type has in a special file. It will generate them at compile time. Currently it can be used in this way:
use unique_type_id::UniqueTypeId;
#[derive(UniqueTypeId)]
struct Test1;
#[derive(UniqueTypeId)]
struct Test2;
assert_eq!(Test1::id().0, 1u64);
assert_eq!(Test2::id().0, 2u64);
It can both generate types using incremental number and use the id from a file.

why to use these weird nesting structure

i'm trying to study the linux kernel and reading the kernel code,
but i can't understand the structure they use for the page structure as shown below:
i mean,why they use union nested in the struct which nested in the union
(the code is simplified...)
struct page {
unsigned long flags;
struct address_space *mapping;
struct {
union {
pgoff_t index;
void *freelist;
};
union {
unsigned counters;
struct {
union {
atomic_t _mapcount;
struct {
unsigned inuse:16;
unsigned objects:15;
unsigned frozen:1;
};
};
atomic_t _count;
};
};
};
}
It is used to bring clarity into the code. It will be easier to read and understand if members are grouped.
Since you are not using the 'sub-structures' in any other data-structure, they are nested. Else, they would be declared separate and included as members, like below:
struct list_based{
pgoff_t index;
void *freelist;
};
struct page {
unsigned long flags;
struct address_space *mapping;
struct list_based lpage;
};
struct sector {
unsigned long sub sect;
struct list_based lsect;
};

Struct->Union-Struct

I have this struct:
struct foo {
int a;
union {
struct {
int b;
struct bar
{
int c;
int d;
} *aBar;
} in;
} u;
};
How I need to declare a variable of type bar, in Visual C++ ?
When you declare an structure like this:
struct
{
int b;
} in;
You are actually creating an object with name in, having unnamed-data type. This data-type would be named internally by compiler, and depends on compiler. The style given above does not declare in to be a type, but a variable!
If you want to make it a type, use either of given approaches:
// Approach 1
struct in{...};
// Approach 2
typedef struct {..} in; // in is now a type, because of `typedef`
If you have compiler that supports C++0x, and specifically type decltype keyword, you can use it against the first style (which makes in a variable). Example:
decltype(in) in_var;
in_var.b = 10;
Thanks Ajay, I solved that way:
foo *k;
decltype(k->u.in.aBar) j;
j->c = 1;
j->d = 1;

Resources