Writing on file using fake mutex class

Writing on file using fake mutex class

Portrait de amina-jarraya

Hello,

I have to coordinate access to write on file from multiple concurrent workers.

So, i use the fake mutex class, but it doesn't work. the file is not correctly wrinting (workers interrupt to write).

cilkscreen::fake_mutex *a = cilkscreen::create_fake_mutex();
a->lock();

gpfgout->printSet(vars.gnprefix_len(), vars.gpprefix_itemset(), nsupport);

a->unlock();
cilkscreen::destroy_fake_mutex(a);

Is it correct to use fake mutex class to write on file ? or there is another class that allows writng on file ?

Thank you in advance,

8 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.
Portrait de Barry Tannenbaum (Intel)

The fake_mutex class isn't intended to be real mutexes. They're... fake. They only signal the Cilkscreen race detector that a race is benign and can be ignored.

From the fake_mutex.h header file:

 * Cilkscreen fake mutexes are provided to indicate to the Cilkscreen race
 * detector that a race should be ignored.

To coordinate between multiple workers or threads, you need to use "real" mutexes.You should look at the mutexes supplied by the OS you're running on (mutex on Linux, CRITICAL_SECTION on Windows). You can use cilkscreen::lock_guard with these if you wrap them in a class which provides the same methods as cilkscreen::fake_mutex.

You should also look at the OS-independent mutexes provided by TBB.

- Barry

Portrait de Barry Tannenbaum (Intel)

I've modified fake_mutex.h to make it clearer that this class does not provide mutual exclusion.

Also, if you're attempting to write to a file from multiple strands, you might want to look at reducer_ostream, which is intended for exactly this situation

- Barry

Portrait de amina-jarraya

Thank you for your answer,

If fake mutex class does not provide mutual exclusion, so could you please show me an example to illustrate how can i use the TBB mutexes to lock/unlock sections (mutual exclusion) ?

I found the follwing example :

#include 
#include
#include

int main()
{
tbb::mutex mut;
int sum = 0;
cilk_for (int i=0; i<10; ++i)
{
mut.lock();
sum = sum + i; // PROTECTED WITH LOCK
mut.unlock();
}
std::cout << "Sum is " << sum << std::endl;

return 0;
}

If this example (cilkMutex.cpp) uses correctly TBB mutex, it doesn't compile, i have this error :

amina@amina-laptop:~/projet/cilkplus-install/examples/test$ icpc -pthread -o cilkMutex cilkMutex.cpp -lcilkrts
/tmp/icpcyhnUVY.o: In function `main':
cilkMutex.cpp:(.text+0x12c): undefined reference to `tbb::internal::handle_perror(int, char const*)'

Have you an idea for this problem ?

Thanks in advance,

Portrait de Barry Tannenbaum (Intel)

Here's one example:

#include 
#include 
#include 
#include 

tbb::spin_rw_mutex countMutex;

int foo()
{
   int z=0;
   cilk_for (int i = 0; i < 10; i++)
   {
       tbb::spin_rw_mutex::scoped_lock lock(countMutex);
       z *= i;
   }
   return z;
}

int main()
{
    foo();
    return 0;
}

Note that support for TBB spinlocks required additional support in Cilkscreen. I confess I'm not sure if we've released that version yet. TBB locks that use OS mutexes should be fine.

- Barry

Portrait de amina-jarraya

Thank you for your quick answer,

I have the following error when compiling your example code :

amina@amina-laptop:~/projet/cilkplus-install/examples/test$ icpc -pthread -o cilkMutex cilkMutex.cpp -lcilkrts
/tmp/icpcMfVTwe.o: In function `__$U0':
cilkMutex.cpp:(.text+0xe8): undefined reference to `tbb::spin_rw_mutex_v3::internal_acquire_writer()'

have you an idea for this error ?

Portrait de Barry Tannenbaum (Intel)
Best Reply

You need to be linking against libtbb.so.

Here's the command I use to build this application on Linux:

icpc -I /home/nsl/bmtannen/project/cilkdev/linux/eng/cilkrts/unix-build/include \
-I /home/nsl/bmtannen/project/cilkdev/linux/eng/cilkrts/unix-build/../include \
-I /home/nsl/bmtannen/project/cilkdev/linux/tbb30_20110427oss/include \
-D TBB_USE_THREADING_TOOLS=1
-L /home/nsl/bmtannen/project/cilkdev/linux/eng/cilkrts/unix-build/linux64/lib -lcilkrts
-L /home/nsl/bmtannen/project/cilkdev/linux/tbb30_20110427oss/build/linux_intel64_gcc_cc4.5.2_libc2.13_kernel2.6.38_debug \
-ltbb_debug \
-o tbb-test tbb-test.cpp

I've pulled down the TBB sources and built them myself.Your path the the TBB librarywill be different. You also probably don't need to specify the directory for the Cilk runtime.

TBB_USE_THREADING_TOOLS needs to be defined to make sure that the notifications that Cilkscreen uses will be present.

- Barry

Portrait de amina-jarraya

Thank you Barry,
It works fine, i use this command :

icpc -D TBB_USE_THREADING_TOOLS=1 -L /home/amina/Bureau/sourcesTBB/tbb30/build/linux_ia32_gcc_cc4.4.3_libc2.11.1_kernel2.6.32_debug/ -ltbb_debug -o TBB TBB.cpp -pthread -lcilkrts

To lock sections, i use the follwing code inside the loop for :

myMutex::scoped_lock lock;
lock.acquire(sm);

z *= i;

lock.release();

Thank you,

Connectez-vous pour laisser un commentaire.