Premium Essay

Nt1310 Unit 1 Reaction Paper

Submitted By
Words 532
Pages 3
#include
#include
#include
#include

typedef int buffer_item; /* declare buffer.h*/
#define BUFFER_SIZE 10
#define SleepDivisor 1000000000

buffer_item buffer[BUFFER_SIZE]; /* the buffer */ int counter; /* buffer counter */

pthread_mutex_t mutex; /* the mutex exclusion lock */ sem_t full,empty; /* the empty, full semaphores */

pthread_t tid; /* define process thread id's */ pthread_attr_t attr; /* set the default process thread attributes */

/* insert item into the buffer */ int insert_item(buffer_item inData) { /* define the insert item method */

if(counter < BUFFER_SIZE) { /* if buffer is empty, insert the items into the buffer */ buffer[counter] = inData; counter++; /* increase …show more content…
return 0; /* return 0 if insert item successful */ } else { return -1; /* return error if insert failure or buffer full */ }
}

/* remove item from the buffer */ int remove_item(buffer_item *outData) { /* define the remove item method */

if(counter > 0) { /* if buffer have items, remove items from buffer */ *outData = buffer[(counter-1)]; counter--; /* decrease the buffer counter */ return 0; /* return 0 if remove item successful */ } else { return -1; /* return error if insert failure or buffer full */ } …show more content…
} else { printf("consumer consumed item %d from buffer at position %d\n",outData,counter); } pthread_mutex_unlock(&mutex); /* release the semaphore mutex lock */ sem_post(&empty);

}
}

/* main method loop */ int main(int argc, char *argv[]) {

if(argc != 4) { /* check the command line input arguments */ fprintf(stderr, "Please provide command line inputs, Example: program \n "); } int SleepTime = atoi(argv[1]); /* define the sleep time for main method before terminating */ int ProdThreadNums = atoi(argv[2]); /* define the number of producer threads */ int ConsThreadNums = atoi(argv[3]); /* define the number of consumer threaads */

int i; /* loop counter variable */ pthread_mutex_init(&mutex, NULL); /* mutex lock create */ sem_init(&full, 0, 0); /* intialize 0 to full semaphore */ sem_init(&empty, 0, BUFFER_SIZE); /*initialize BUFFER_SIZE to empty semaphore */ pthread_attr_init(&attr); /* thread default attributes */ counter = 0; /* initialize 0 to counter value */

for(i = 0; i < ProdThreadNums; i++) { pthread_create(&tid,&attr,producer_thread,NULL); /* create producer threads up to defined number

Similar Documents

Premium Essay

Nt1310 Unit 1 Reaction Paper

...The goals of this solution were to not only reimplement GPUs into SIA but to also ensure that the implementation would be extensible in the future as well as easier to use for the SIAL programmer. Previously, in ACES III, the GPU implementation required specific actions by the SIAL programmer to move and allocate data. This new solution will automatically handle this by integrating the GPU operations into the block operations more naturally. At a high level, this implementation handles data lazily but could be easily changed to proactively transfer and allocate data through look-ahead optimizations in the SIAL compiler. The data transfers and allocations are considered lazy because they wait until an operation needs them, at which point, the data will be allocated or transferred. To implement this data management, we built on top of the Block class in SIA. Each block represents a tensor and therefore has the array of data making up the tensor as well as some other administrative information associated with it. Previously, in ACES III, this Block class had two data pointers: one pointer to the host data and another pointer to GPU data if it is available. In this new solution, we use a map and OpenCL to track on what devices a block’s data can be found. Since OpenCL allows for executing code on multiple types of devices, we chose to use it for determining what data is on what device as well as the current device on which we are currently working. By using OpenCL in this way, we...

Words: 812 - Pages: 4