#include <maya/MSimple.h>
#include <maya/MGlobal.h>
#include <maya/MIOStream.h>
#include <maya/MTimer.h>
#include <maya/MMutexLock.h>
#include <maya/MSpinLock.h>
#include <maya/MAtomic.h>
#include <maya/MThreadUtils.h>
#ifdef MAYA_TBB
#include <tbb/blocked_range.h>
#include <tbb/parallel_for.h>
#endif
int doMutexCalc(int iterations, int val)
{
int sum = 0;
#if defined(MAYA_TBB)
tbb::parallel_for(tbb::blocked_range<int>(0, iterations),
[&](const tbb::blocked_range<int> &br) {
for (int i = br.begin(); i < br.end(); i++) {
#else
#ifdef _OPENMP
#pragma omp parallel for
#endif
for(int i=0; i<iterations; i++) {
#endif
sum += val;
}
#if defined(MAYA_TBB)
});
#endif
return sum;
}
int doSpinCalc(int iterations, int val)
{
int sum = 0;
#if defined(MAYA_TBB)
tbb::parallel_for(tbb::blocked_range<int>(0, iterations),
[&](const tbb::blocked_range<int> &br) {
for (int i = br.begin(); i < br.end(); i++) {
#else
#ifdef _OPENMP
#pragma omp parallel for
#endif
for(int i=0; i<iterations; i++) {
#endif
sum += val;
}
#if defined(MAYA_TBB)
});
#endif
return sum;
}
int doAtomicCalc(int iterations, int val)
{
int sum = 0;
#if defined(MAYA_TBB)
tbb::parallel_for(tbb::blocked_range<int>(0, iterations),
[&](const tbb::blocked_range<int> &br) {
for (int i = br.begin(); i < br.end(); i++) {
#else
#ifdef _OPENMP
#pragma omp parallel for
#endif
for(int i=0; i<iterations; i++) {
#endif
}
#if defined(MAYA_TBB)
});
#endif
return sum;
}
int doReductionCalc(int iterations, int val)
{
int sum = 0;
#if defined(MAYA_TBB)
tbb::parallel_for(tbb::blocked_range<int>(0, iterations),
[&](const tbb::blocked_range<int> &br) {
for (int i = br.begin(); i < br.end(); i++) {
#else
#ifdef _OPENMP
#pragma omp parallel for reduction(+:sum)
#endif
for(int i=0; i<iterations; i++) {
#endif
sum += val;
}
#if defined(MAYA_TBB)
});
#endif
return sum;
}
int doUnsafeCalc(int iterations, int val)
{
int sum = 0;
#if defined(MAYA_TBB)
tbb::parallel_for(tbb::blocked_range<int>(0, iterations),
[&](const tbb::blocked_range<int> &br) {
for (int i = br.begin(); i < br.end(); i++) {
#else
#ifdef _OPENMP
#pragma omp parallel for
#endif
for(int i=0; i<iterations; i++) {
#endif
sum += val;
}
#if defined(MAYA_TBB)
});
#endif
return sum;
}
int doSerialCalc(int iterations, int val)
{
int sum = 0;
for(int i=0; i<iterations; i++) {
sum += val;
}
return sum;
}
{
if ( MS::kSuccess != stat )
cout<<"Error creating curve."<<endl;
MString str =
MString(
"Invalid number of arguments, usage: threadingLockTests 1000000");
}
int iterations = args.
asInt( 0, &stat );
int increment = 2;
int sum = 0;
int repeat = 1;
for(int i=0; i<repeat; i++) {
sum = doMutexCalc(iterations, increment);
if(sum != iterations*increment) {
printf("doMutexCalc failed, sum %d\n", sum);
break;
}
}
for(int i=0; i<repeat; i++) {
sum = doSpinCalc(iterations, increment);
if(sum != iterations*increment) {
printf("doSpinCalc failed, sum %d\n", sum);
break;
}
}
for(int i=0; i<repeat; i++) {
sum = doAtomicCalc(iterations, increment);
if(sum != iterations*increment) {
printf("doAtomicCalc failed, sum %d\n", sum);
break;
}
}
for(int i=0; i<repeat; i++) {
sum = doReductionCalc(iterations, increment);
if(sum != iterations*increment) {
printf("doReductionCalc failed, sum %d\n", sum);
break;
}
}
for(int i=0; i<repeat; i++) {
sum = doUnsafeCalc(iterations, increment);
if(sum != iterations*increment) {
printf("doUnsafeCalc failed, sum %d\n", sum);
break;
}
}
for(int i=0; i<repeat; i++) {
sum = doSerialCalc(iterations, increment);
if(sum != iterations*increment) {
printf("doUnthreadedCalc failed, sum %d\n", sum);
break;
}
}
return stat;
}