first time looking at phtread

in fiasco&l4, they are using phtreads to manage the threads. the learning material can be found here > https://computing.llnl.gov/tutorials/pthreads/

been trying to learning the usage of phtreads, mainly by looking at the example and the doc of pthreads and the l4re api, still dont get the grasp of it. been trying to make a code where i can compare the speed of using the threads vs the speed of not using the threads. so far the result is using the threads tend to be more slower, which is maybe because the threads are actually still using the same processor so they are not really a thread. still trying to understand how it goes.

here is my code

#include <l4/sys/ipc.h>
#include <pthread-l4.h>
#include <unistd.h>

#include <sys/time.h>
#include <stdio.h>
#include <math.h>

#define NUM_THREADS 5
#define NUM_OF_OPERATION 10

static pthread_t t_server;

/* Return 1 if the difference is negative, otherwise 0.  */
int timeval_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1)
{
long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) – (t1->tv_usec + 1000000 * t1->tv_sec);
result->tv_sec = diff / 1000000;
result->tv_usec = diff % 1000000;

return (diff<0);
}

void timeval_print(struct timeval *tv)
{
char buffer[30];
time_t curtime;

curtime = tv->tv_sec;
strftime(buffer, 30, “%m-%d-%Y  %T”, localtime(&curtime));
}
static void *fn_server(void *arg)
{
struct timeval tvBegin, tvEnd, tvDiff;

// begin
gettimeofday(&tvBegin, NULL);
timeval_print(&tvBegin);

printf(“entering thread server\n”);
l4_msgtag_t tag;
l4_umword_t label;
int ipc_error;
int totalAnswer=0;
int acceptedT=0;
(void)arg;
tag = l4_ipc_wait(l4_utcb(), &label, L4_IPC_NEVER);
while(acceptedT<NUM_THREADS)
{
ipc_error = l4_ipc_error(tag, l4_utcb());
if (ipc_error)
{
fprintf(stderr, “t server IPC Error: %x\n”, ipc_error);
continue;
}
if ( l4_utcb_mr()->mr[1] >0)
{
totalAnswer+= l4_utcb_mr()->mr[0];
acceptedT++;
printf(“SERVER: Thread sender id: %ld\n”,l4_utcb_mr()->mr[1]);
printf(“SERVER: Accepted value: %ld\n”,l4_utcb_mr()->mr[0]);
l4_utcb_mr()->mr[0]=0;
l4_utcb_mr()->mr[1]=0;
if(acceptedT==NUM_THREADS)
{
gettimeofday(&tvEnd, NULL);
timeval_print(&tvEnd);

// diff
timeval_subtract(&tvDiff, &tvEnd, &tvBegin);
printf(“SERVER: Total Result: %d\nSERVER: Total Accepted Thread: %d\n”, totalAnswer, acceptedT);
printf(“SERVER: elapsed time= %ld.%06ld\n”, tvDiff.tv_sec, tvDiff.tv_usec);
}

tag = l4_ipc_reply_and_wait(l4_utcb(), l4_msgtag(0,2,0,0),&label, L4_IPC_NEVER);
}
}

printf(“SERVER: Exiting\n”);
pthread_exit(NULL);
}
void *fn_calculate(void *tid)
{
int threadId;
threadId = (int)tid;
l4_msgtag_t tag;
int ipc_error;

struct timeval tvBegin, tvEnd, tvDiff;

// begin
gettimeofday(&tvBegin, NULL);
timeval_print(&tvBegin);
printf(“entering thread %d\n”, threadId);

long j=0;
int i,k ;
int k_begin = (NUM_OF_OPERATION/NUM_THREADS)*(threadId-1);
int k_end = NUM_OF_OPERATION/NUM_THREADS*threadId;
for(k=
k_begin;
k<k_end;++k) {
for(i=0;i<999999;++i) {
j+=sqrt(sqrt(i))-k;
}
}
printf(“thread %d: result %ld calculation range %d – %d\n”,threadId, j,k_begin, k_end);
l4_utcb_mr()->mr[0] = j;
l4_utcb_mr()->mr[1] = threadId;
tag = l4_ipc_call(pthread_getl4cap(t_server),l4_utcb(),
l4_msgtag(0,2,0,0), L4_IPC_NEVER);
ipc_error = l4_ipc_error(tag, l4_utcb());
if (ipc_error)
{
fprintf(stderr, “thread %d: IPC Error %x\n”,
threadId, ipc_error);
}
else
{
printf(“thread %d: sending is succesful \n”,threadId);
}

printf(“thread %d: exiting \n”,threadId);
gettimeofday(&tvEnd, NULL);
timeval_print(&tvEnd);

// diff
timeval_subtract(&tvDiff, &tvEnd, &tvBegin);
printf(“thread %d: elapsed time= %ld.%06ld\n”, threadId, tvDiff.tv_sec, tvDiff.tv_usec);
pthread_exit(NULL);
}
int main()
{
struct timeval tvBegin, tvEnd, tvDiff;

pthread_t threads[NUM_THREADS];
// begin
gettimeofday(&tvBegin, NULL);
timeval_print(&tvBegin);

long p=0;
int r,q ;

for(q=0;
q<NUM_OF_OPERATION;++q) {
for(r=0;r<999999;++r) {
p+=sqrt(sqrt(r))-q;
}
}
printf(“MAIN THREAD: result from not using thread %ld\n”,p);

//end
gettimeofday(&tvEnd, NULL);
timeval_print(&tvEnd);

// diff
timeval_subtract(&tvDiff, &tvEnd, &tvBegin);
printf(“MAIN THREAD: elapsed time= %ld.%06ld\n”, tvDiff.tv_sec, tvDiff.tv_usec);

pthread_create(&t_server, NULL, fn_server, NULL);
for(int t=1;t<=NUM_THREADS;t++)
{
pthread_create(&threads[t], NULL, fn_calculate, (void *) t);
}

printf(“MAIN THREAD: Exiting”);
pthread_exit(NULL);
}

Leave a Reply

Your email address will not be published. Required fields are marked *