I am going to be working on a CUDA gpu based chess engine, and I will be using an unknown number of cpu and gpu threads, possibly hundreds or thousands of threads. The thing i want to do is create objects' names using stringstream and the Boost library.
*Edit: All the gpu threads will be handled by CUDA. But I still need to be able to create cpu threads systematically without writing out each of their names.
class cl{};
// instead of
cl Ex_1;
cl Ex_2;
cl Ex_3;
cl Ex_4;
// or
int N_1;
int N_2;
int N_3;
int N_4;
// I would like to use
for (int i = 0; i < 4; ++i)
{
cl Ex_i;
// or
int N_i;
}
// And then work with those.
for (int i = 0; i < 4; ++i)
{
Ex_i.Something = Whatever;
// or
N_i = IDK;
}
Creating a variable whose name is not known at compile time is not possible. However, I don't see why you couldn't use a container such as vector (if indexes are enough) or a map / hash table to store your variables.
class cl{};
// instead of
cl Ex_1;
cl Ex_2;
cl Ex_3;
cl Ex_4;
// or
int N_1;
int N_2;
int N_3;
int N_4;
// I would like to use
for (int i = 0; i < 4; ++i)
{
cl Ex_i;
// or
int N_i;
}
// And then work with those.
for (int i = 0; i < 4; ++i)
{
Ex_i.Something = Whatever;
// or
N_i = IDK;
}
class cl{};
// instead of
cl Ex[4];
// or
int N[4];
// I would like to use
for (int i = 0; i < 4; ++i)
{
Ex[i] = ...;
// or
N[i] = ...;
}
// And then work with those.
for (int i = 0; i < 4; ++i)
{
Ex[i].Something = Whatever;
// or
N[i] = IDK;
}
What i was doing was creating object based CPU threads and my focus was the objects, not the threads. I don't know if my program will have 1 main thread and 2 worker threads, or if it will have 6 worker threads, the number of threads will depend on the user preference. I know how to use arrays and vectors, it's the unknown number of threads I am trying to accomplish. When I write programs in CUDA, I can create a kernel with a variable number of threads like:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "device_launch_parameters.h"
#include <cuda_runtime.h>
__global__ void GPUfunction ()
{
// Task for a GPU thread.
// Uses its thread ID to know what to do.
}
int main ()
{
GPUfunction <<<x, y>>> (); // x number of blocks, y number of threads per block.
}
But I can't, or at least don't know how to, systematically create threads without naming them individually in the manner i showed before. I am also using the Boost library to do this if I haven't already mentioned it.