From different languages I am used to arrays, here in C/C++ it is a bit harder. Can you help? I am writing C code now. I have file which uses openCL to generate program for GPU. Now, there is a part of code which detects some informations about how many devices there is in the machine (I dont know how this exactly works. The complete code is long so I paste the code here: http://paste.ofcode.org/U5g2Z3Cj5ZuRBnJf2b5cqv
and here the key part - this is how they use it:
Now I am writing the code to write the binaries data, and this is where I need to find out how to calculate the count or "how many times the loop should be repeated". So if you have two GPU devices in machine (as far as I understand openCL) the binaries data should contain two sets of data for each GPU device. So I have this:
1 2 3 4 5 6 7 8 9 10 11 12 13
void write(char ** binaries, size_t nDevices){
char str[1000];
int n;
for( n = 0; n < nDevices; n++ )
{
FILE* f = fopen("input.bin", "wb");
fseek(f, 0, SEEK_SET);
int i;
for (i = 0; i < jel_size; i++)
fwrite(&foo[i], sizeof(struct foo), 1, f);
fclose(f);
}
}
Also I need advice how to turn the n to valid filename for fopen.
Any time you see type* you are looking at a pointer to type, which can be treated as if it were an array of type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
void print5( int* xs ) // xs is a pointer to an integer (which we'll treat like an array)
{
for (int n = 0; n < 5; n++)
std::cout << xs[ n ] << " ";
std::cout << "\n";
}
int main()
{
int a[] = { 1, 2, 3, 4, 5 };
print5( &(a[0]) ); // pass as argument the address to the first element of a[]
}
Any time you see type** you are looking at a pointer to a pointer to type.
Now, the array thing again: you may have an array of pointers to type, or a pointer to an array of type, or an array of arrays of type.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
void print2( int** xs ) // xs here is a pointer to pointer to int, or an array of pointers to int...
{
for (int n = 0; n < 2; n++)
print5( xs[ n ] );
}
int main()
{
int a[] = { 1, 2, 3, 4, 5 };
int b[] = { 6, 7, 8, 9, 10 };
int* xs[] = { &(a[0]), &(a[1]) };
print2( &(xs[0]) ); // the address of the first element of a[] is a pointer to a pointer to int.
}