I want to multithread a for loop for some of my C code, but I'm not really sure how to a create a bunch of private arrays for each thread in C. When I use C++. I just give it like; #pragma omp parallel firstprivate(std::vector), but I'm not sure how I do it in C as I pass a ptr that points to the array to the function as an argument. If I were to just do firstprivate(int* array). I would then get a bunch of ptrs that all point to the same array, but I won't get a bunch of threads that all work on their separate arrays.
My problem looks something like;
void pbfs(int n, int* ver, int* edges, int* p, int* dist, int* S, int* T)
{
int i, j; // Loop indices
int v, w; // Pointers to vertices
int num_r, num_w; // Number of vertices in S and T, respectively
int* temp; // Temporary pointer
...
while (num_r != 0) { // Loop until all vertices have been discovered
for (i = 0; i < num_r; i++) { // Loop over vertices in S
v = S[i]; // Grab next vertex v in S
for (j = ver[v]; j < ver[v + 1]; j++) { // Go through the neighbors of v
w = edges[j]; // Get next neighbor w of v
if (p[w] == -1) { // Check if w is undiscovered
p[w] = v; // Set v as the parent of w
dist[w] = dist[v] + 1; // Set distance of w
T[num_w++] = w; // Add w to T and increase number of vertices discovered
}
} // End loop over neighbors of v
} // End loop of vertices in S
temp = S; // Swap S and T
S = T;
T = temp;
num_r = num_w; // Set number of elements in S
num_w = 0; // Set T as empty
} // End loop over entire graph
}
I'm thinking that having a pragma for the 2nd for loop would speed up this function. The problem is that T[num_w++] = w; is not safe. Was thinking each thread having their own copy of T, then merge T. I'm not exactly sure how to merge all the T's after either.