Question

My algorithm computes the vertices of a shape in 3-dimensional space by using a 2-dimensional loop iterating over the U and V segments.

for (LONG i=0; i < info.useg + 1; i++) {
    // Calculate the u-parameter.
    u = info.umin + i * info.udelta;
    for (LONG j=0; j < info.vseg + 1; j++) {
        // Calculate the v-parameter.
        v = info.vmin + j * info.vdelta;

        // Compute the point's position.
        point = calc_point(op, &info, u, v);

        // Set the point to the object and increase the point-index.
        points[point_i] = point;
        point_i++;
    }
}

The array of points is however a one-dimensional array, which is why point_i is incremented in each loop. I know that I could compute the index via point_i = i * info.vseg + j.

I want this loop to be multithreaded. My aim was to create a number of threads that all process a specific range of points. In the thread, I'd do it like:

for (LONG x=start; x <= end; x++) {
    LONG i = // ...
    LONG j = // ...

    Real u = info.umin + i * info.udelta;
    Real v = info.vmin + j * info.vdelta;

    points[i] = calc_point(op, &info, u, v);
}

The problem is to calculate the i and j indecies from the linear point-index. How can I compute i and j when (well I think):

point_i = i * vsegments + j

I'm unable to solve the math, here..

Was it helpful?

Solution

point_i = i * vsegments + j gives you:

i = point_i / vsegments
j = point_i % vsegments

Of course your loops actually do segments + 1 iterations each (indices 0 to segments), so you would need to use vsegments + 1 instead of vsegments

As a side note: Do you actually need to merge the loops into one for multithreading? I would expect the outer loop to have typically enough iterations to saturate your availible cores anyways.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top