#include <math.h>
#include <maya/MIOStream.h>
#include <maya/MSimple.h>
#include <maya/MTimer.h>
#include <maya/MGlobal.h>
#include <maya/MThreadPool.h>
typedef struct _threadDataTag
{
int threadNo;
long primesFound;
long start, end;
} threadData;
typedef struct _taskDataTag
{
long start, end, totalPrimes;
} taskData;
#define NUM_TASKS 16
static bool TestForPrime(int val)
{
int limit, factor = 3;
limit = (long)(sqrtf((float)val)+0.5f);
while( (factor <= limit) && (val % factor))
factor ++;
return (factor > limit);
}
MThreadRetVal Primes(void *data)
{
threadData *myData = (threadData *)data;
for( int i = myData->start + myData->threadNo*2; i <= myData->end; i += 2*NUM_TASKS )
{
if( TestForPrime(i) )
myData->primesFound++;
}
return (MThreadRetVal)0;
}
void DecomposePrimes(void *data, MThreadRootTask *root)
{
taskData *taskD = (taskData *)data;
threadData tdata[NUM_TASKS];
for( int i = 0; i < NUM_TASKS; ++i )
{
tdata[i].threadNo = i;
tdata[i].primesFound = 0;
tdata[i].start = taskD->start;
tdata[i].end = taskD->end;
}
for( int i = 0; i < NUM_TASKS; ++i )
{
taskD->totalPrimes += tdata[i].primesFound;
}
}
int SerialPrimes(int start, int end)
{
int primesFound = 0;
for( int i = start; i <= end; i+=2)
{
if( TestForPrime(i) )
primesFound++;
}
return primesFound;
}
int ParallelPrimes(int start, int end)
{
return 0;
}
taskData tdata;
tdata.totalPrimes = 0;
tdata.start = start;
tdata.end = end;
return tdata.totalPrimes;
}
{
MString introStr =
MString(
"Computation of primes using the Maya API");
MString str =
MString(
"Invalid number of arguments, usage: threadTestCmd 1 10000");
}
int start = args.
asInt( 0, &stat );
if ( MS::kSuccess != stat ) {
MString str =
MString(
"Invalid argument 1, usage: threadTestCmd 1 10000");
}
int end = args.
asInt( 1, &stat );
if ( MS::kSuccess != stat ) {
MString str =
MString(
"Invalid argument 2, usage: threadTestCmd 1 10000");
}
if((start % 2) == 0 ) start++;
int serialPrimes = SerialPrimes(start, end);
int parallelPrimes = ParallelPrimes(start, end);
if ( serialPrimes != parallelPrimes ) {
MString str(
"Error: Computations inconsistent");
}
if(parallelTime>0.0) {
double ratio = serialTime/parallelTime;
str +=
MString(
"Elapsed time for parallel computation: ") + parallelTime +
MString(
"s\n");
} else {
MString str =
MString(
"\nParallel time zero, no scaling measurement possible\n");
}
}