Using Struct in OpenCL - opencl

I want to initialize a data structure in OpenCL. In C++, the initialization of the structure is like:
typedef struct mystruct{
float x;
float y;
float z;
mystruct(){
x = 0.0;
y = 0.0;
z = 0.0;
}
}mystruct;
How to do this in OpenCL ?

You typedef the struct the same way as in C and then initialize it with
mystruct foo = {1.0, 2.1, 3.2};
If you want the default constructor like behaviour of C++ you could just write a function that looks like
mystruct
initMystruct ()
{
mystruct foo = {0., 0., 0.};
return foo;
}

Related

Extension type instance inside CPython extension type

I have the following extension types in C
//VECTOR 3
typedef struct
{
PyObject_HEAD
double x;
double y;
double z;
}Vec3Object;
static PyMemberDef Vec3Object_Members[]=
{
{"x", T_DOUBLE, offsetof(Vec3Object, x), 0, "X component"},
{"y", T_DOUBLE, offsetof(Vec3Object, y), 0, "Y component"},
{"z", T_DOUBLE, offsetof(Vec3Object, z), 0, "Z component"},
{NULL}
};
//TRANSFORMATIONS
typedef struct
{
PyObject_HEAD
Vec3Object position;
Vec3Object rotation;
Vec3Object scale;
}TransformObject;
//ENTITY
typedef struct
{
PyObject_HEAD
TransformObject transform;
}EntityObject;
And the following PyTypeObject(its also the same for the others and their respective members):
static PyTypeObject TransformObject_Type=
{
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "scene.Transform",
.tp_doc = "Transform type",
.tp_basicsize = sizeof (TransformObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_new = PyType_GenericNew,
.tp_members = TransformObject_Members,
};
Creating a Vec3 works fine but how would a PyMemberDef for TransformObject and EntityObject be defined?
I basically want to call the following Python code:
entity.transform.position.x = 12.5
entity.transform.rotation.y = -74.0
Or the following methods once in a PyMethodDef:
Vec3.Length(entity.transform.position)

llvm: create struct type array (pointer)

I'm working with llvm (frontend: C++) and trying to implement the struct type array (struct type pointer) in my language. My struct looks like this:
struct myStruct {
double const *myArray; //double type array
char const *myString; //string
};
And my code for struct type array implementation:
...
auto myString = Type::getInt8PtrTy(TheContext);
auto myArray = Type::getDoublePtrTy(TheContext);
auto myStruct = StructType::create(TheContext, "Struct");
myStruct->setBody({myString, myArray});
return PointerType::get(myStruct, 0);
I think that this way isn't correct because when I'm assigning data to the pointer and trying to print the array elements I got only first struct array element and only "myString" ("myArray" is always empty). I want to know what I'm doing wrong.
Additional code:
struct myStruct* getData(){
double const arr1[3] = { 10, 7, 9 }; double const* arraypnt1 = arr1; char const *str1 = "string1";
double const arr2[3] = { 1, 2, 4}; double const* arraypnt2 = arr2; char const *str2 = "string2";
struct myStruct p[2] = {{ arraypnt1, str1 }, { arraypnt2, str2 }};
struct myStruct* pairarr = p; // array of pairs
return pairarr;
}
//code generator
Value *ASTVarExpression::generateCode() {
Function *function = Builder.GetInsertBlock()->getParent();
Value *initValue = VariableValue->generateCode(); //this line creates call to function 'getData()'
if (!initValue) { return nullptr; }
AllocaInst *alloca = CreateFunctionEntryAlloca(function, VariableName, GetType(DeclaredType));
Builder.CreateStore(initValue, alloca); //store 'getData()' result in variable which type is 'myStructure* '
NamedValues[VariableName] = alloca;
return initValue;
}
static AllocaInst *CreateFunctionEntryAlloca(Function *function, StringRef variableName, Type* variableType) {
IRBuilder<> tempBlock(&function->getEntryBlock(), function->getEntryBlock().begin());
return tempBlock.CreateAlloca(variableType, 0, variableName);
}
Type *GetType(int expressionType){ //returns struct array type
auto myString = Type::getInt8PtrTy(TheContext);
auto myArray = Type::getDoublePtrTy(TheContext);
auto myStruct = StructType::create(TheContext, "Struct");
myStruct->setBody({myString, myArray});
return PointerType::get(myStruct, 0);
}
llvm IR code:
%varName = alloca %Struct.0*, align 8
%gencall = call %Struct* #getData()
store %Struct* %gencall, %Struct.0** %varName, align 8
ret %Struct* %gencall

constant int and pointers example

int main() {
const int i = 1;
const int* p = &i;
int j = 2;
const int* q = &j;
j = 3;
printf("%d", *p + *q);
return 0;
}
I have this code, and I try to understand how it compiles. p and q are pointers to constant integers but j isn't declared as constant. Moreover, j changes into 3.
How does it work?
Thanks!
On the 5th line, you're assigning the address of variable j to q. This do not enforce any constraint over j, just on pointer q. Through q the compiler won't allow you to change pointed value, however j remains writeable and the line j = 3; is legal.
See What is the difference between const int*, const int * const, and int const *?

C pointer and function pointer problems

My enviroment is fedora17 64bit
Pointer:
int a[5] = {1,2,3,4,5};
int *p = (*int)(&a+1);
The value of *(p-1) is 5.
Assume &a is 0x7fffffffdf50.
I wonder know why (&a+1) is 0x7fffffffdf64 and why *(p-1) is 5?
Function pointer:
Re-write
void(*(*(fptr[5])(char*);
to
typedef_______?_______;
pf(*fptr)[5];
Correct your pointer assignment as
int *p = (int*)(&a+1);

pointer to arrays of struct

struct a{
double array[2][3];
};
struct b{
double array[3][4];
};
void main(){
a x = {{1,2,3,4,5,6}};
b y = {{1,2,3,4,5,6,7,8,9,10,11,12}};
}
I have two structs, inside which there are two dim arrays with different sizes. If I want to define only one function, which can deal with both x and y (one for each time), i.e., the function allows both x.array and y.array to be its argument. How can I define the input argument? I think I should use a pointer.... But **x.array seems not to work.
For example, I want to write a function PrintArray which can print the input array.
void PrintArray( ){}
What should I input into the parenthesis? double ** seems not work for me... (we can let dimension to be the PrintArray's argument as well, telling them its 2*3 array)
Write a function that takes three parameters: a pointer, the number of rows, and the number of columns. When you call the function, reduce the array to a pointer.
void PrintArray(const double *a, int rows, int cols) {
int r, c;
for (r = 0; r < rows; ++r) {
for (c = 0; c < cols; ++c) {
printf("%3.1f ", a[r * cols + c]);
}
printf("\n");
}
}
int main(){
struct a x = {{{1,2,3},{4,5,6}}};
struct b y = {{{1,2,3,4},{5,6,7,8},{9,10,11,12}}};
PrintArray(&x.array[0][0], 2, 3);
PrintArray(&y.array[0][0], 3, 4);
return 0;
}

Resources