bp_1986
June 22nd, 2011, 11:36 AM
Hi everybody. I'm developing a multithreaded program in SIMD style, so I'm partitioning an array of, say, N elements in N_thread pieces, each piece made of thread_dim elements. Here is a simplified version of a thread code:
void my_thread( int* array, const int* shared_data, int N, int thread_dim, int thread_id)
{
// ...
for(int i=0; i<thread_dim; ++i)
{
int index = thread_id * thread_dim + i;
if( index < N)
array[index] = shared_data[0] + index;
}
// ...
}
thread_id is 0,1,2,...,N_thread-1 so thread_id*thread_dim is an offset that makes impossible race conditions between threads, so threads share the same pointer but they never access the same position. My question is: is there some kind of hidden synchronization added by OS in pointer accesses, also if threads are accessing different - maybe contiguous - elements? Writing const int* should avoid synchronization problems (if any) when threads access the same position but just to read from?
In addition, I'm compiling everything on Windows XP, Visual Studio 2008 and I'll do the same on Linux, I use Boost Threads.
Thanks to everyone would be so nice to answer!
Biagio
void my_thread( int* array, const int* shared_data, int N, int thread_dim, int thread_id)
{
// ...
for(int i=0; i<thread_dim; ++i)
{
int index = thread_id * thread_dim + i;
if( index < N)
array[index] = shared_data[0] + index;
}
// ...
}
thread_id is 0,1,2,...,N_thread-1 so thread_id*thread_dim is an offset that makes impossible race conditions between threads, so threads share the same pointer but they never access the same position. My question is: is there some kind of hidden synchronization added by OS in pointer accesses, also if threads are accessing different - maybe contiguous - elements? Writing const int* should avoid synchronization problems (if any) when threads access the same position but just to read from?
In addition, I'm compiling everything on Windows XP, Visual Studio 2008 and I'll do the same on Linux, I use Boost Threads.
Thanks to everyone would be so nice to answer!
Biagio