260 lines
6.3 KiB
C
260 lines
6.3 KiB
C
#define _GNU_SOURCE
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <pthread.h>
|
|
#include <stdint.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <limits.h>
|
|
#include <sys/stat.h>
|
|
#include <string.h>
|
|
|
|
/* Non standards includes */
|
|
#include <papihighlevel.h>
|
|
#include <commtech.h>
|
|
#include <specific_comm.h>
|
|
|
|
static long nb_cache_lines = 0;
|
|
long nb_prod = 0;
|
|
static long size_buf = 1;
|
|
static char *calculation_lib = NULL;
|
|
static int shared = 0;
|
|
pthread_cond_t cond_cons_has_finished = PTHREAD_COND_INITIALIZER;
|
|
pthread_mutex_t mutex_cons_has_finished = PTHREAD_MUTEX_INITIALIZER;
|
|
static int consumer_has_finished = 0;
|
|
static int producers_ended = 0;
|
|
|
|
void usage(char *argv[])
|
|
{
|
|
char format[] = "-n [options]";
|
|
char options[] = "Required options :\n"
|
|
"-n nb_cache_lines\tNumber of cache lines to send to another core\n"
|
|
"-p nb_producers\tNumber of producers which send data to another core\n"
|
|
"Facultative options :\n"
|
|
"-h\t\t\tPrint this help\n"
|
|
"-s\t\t\tShare the same L2 cache or not\n"
|
|
"-c calculation_lib\tLibrary to use for calculation\n"
|
|
"\t\t\tThis library must implement functions in calc.h\n";
|
|
printf("Usage : %s %s\n", argv[0], format);
|
|
printf("Options :\n");
|
|
printf("%s\n", options);
|
|
}
|
|
|
|
int analyse_options(int argc, char *argv[])
|
|
{
|
|
int opt;
|
|
|
|
opterr = 0;
|
|
while ((opt = getopt(argc, argv, ":hsc:n:p:b:")) != -1)
|
|
{
|
|
switch (opt)
|
|
{
|
|
case 'b' :
|
|
{
|
|
char *inval;
|
|
size_buf = strtol(optarg, &inval, 10);
|
|
if ((*optarg == '\0') || (*inval != '\0'))
|
|
{
|
|
fprintf(stderr, "Option '-b' needs an integer argument\n");
|
|
return -1;
|
|
}
|
|
if ((nb_cache_lines <= 0) || ((nb_cache_lines == LONG_MAX) && errno == ERANGE))
|
|
{
|
|
fprintf(stderr, "Number of cache lines for each buffer must be between 1 and %ld, both inclusive\n", LONG_MAX);
|
|
return -1;
|
|
}
|
|
}
|
|
break;
|
|
case 'c' :
|
|
calculation_lib = optarg;
|
|
{
|
|
struct stat file_stat;
|
|
if (stat(calculation_lib, &file_stat))
|
|
{
|
|
printf("%s: %s\n", optarg, strerror(errno));
|
|
return -1;
|
|
}
|
|
}
|
|
break;
|
|
case 'h' :
|
|
usage(argv);
|
|
exit(EXIT_SUCCESS);
|
|
case 'n' :
|
|
{
|
|
char *inval;
|
|
nb_cache_lines = strtol(optarg, &inval, 10);
|
|
if ((*optarg == '\0') || (*inval != '\0'))
|
|
{
|
|
fprintf(stderr, "Option '-n' needs an integer argument\n");
|
|
return -1;
|
|
}
|
|
if ((nb_cache_lines <= 0) || ((nb_cache_lines == LONG_MAX) && errno == ERANGE))
|
|
{
|
|
fprintf(stderr, "Number of cache lines to be sent must be between 1 and %ld, both inclusive\n", LONG_MAX);
|
|
return -1;
|
|
}
|
|
}
|
|
break;
|
|
case 'p' :
|
|
{
|
|
char *inval;
|
|
nb_prod = strtol(optarg, &inval, 10);
|
|
if ((*optarg == '\0') || (*inval != '\0'))
|
|
{
|
|
fprintf(stderr, "Option '-p' needs an integer argument\n");
|
|
return -1;
|
|
}
|
|
if ((nb_cache_lines <= 0) || ((nb_cache_lines == LONG_MAX) && errno == ERANGE))
|
|
{
|
|
fprintf(stderr, "Number of producers must be between 1 and %ld, both inclusive\n", LONG_MAX);
|
|
return -1;
|
|
}
|
|
}
|
|
break;
|
|
case 's' :
|
|
shared = 1;
|
|
/* TODO: shared L2 cache */
|
|
break;
|
|
case '?' :
|
|
fprintf(stderr, "Option inconnue\n");
|
|
return -1;
|
|
case ':' :
|
|
fprintf(stderr, "Option %s needs an argument\n", argv[optind]);
|
|
return -1;
|
|
default :
|
|
fprintf(stderr, "Error while analysing command line options\n");
|
|
return -1;
|
|
}
|
|
}
|
|
if (!nb_cache_lines)
|
|
{
|
|
fprintf(stderr, "You must give the number of cache lines to be sent\n");
|
|
return -1;
|
|
}
|
|
if (!nb_prod)
|
|
{
|
|
fprintf(stderr, "You must give the number of producers\n");
|
|
return -1;
|
|
}
|
|
if (shared && (nb_prod > 1))
|
|
{
|
|
fprintf(stderr, "Too many producers to fit with the consumer in processors which share a same cache\n");
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void *producer(void *unused)
|
|
{
|
|
int i, j;
|
|
void *k;
|
|
|
|
init_producer_thread();
|
|
if (shared)
|
|
{
|
|
pthread_t tid;
|
|
cpu_set_t cpuset;
|
|
|
|
tid = pthread_self();
|
|
CPU_ZERO(&cpuset);
|
|
CPU_SET(1, &cpuset);
|
|
if (pthread_setaffinity_np(tid, sizeof(cpu_set_t), &cpuset))
|
|
{
|
|
perror("pthread_setaffinity_np");
|
|
return NULL;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
pthread_t tid;
|
|
cpu_set_t cpuset;
|
|
|
|
tid = pthread_self();
|
|
CPU_ZERO(&cpuset);
|
|
CPU_SET(2, &cpuset);
|
|
if (pthread_setaffinity_np(tid, sizeof(cpu_set_t), &cpuset))
|
|
{
|
|
perror("pthread_setaffinity_np");
|
|
return NULL;
|
|
}
|
|
}
|
|
k = (void *) 0x6384923;
|
|
if (initialize_papi() != -1)
|
|
{
|
|
for(i = 0; i < nb_cache_lines; i++) {
|
|
//printf("[%p] Send %d new CACHE_LINE\n", (void *) pthread_self(), BUF_SIZE / CACHE_LINE_SIZE);
|
|
for(j = 0; j < (BUF_SIZE / sizeof(uintptr_t)); j++)
|
|
send(&k);
|
|
}
|
|
print_results(BUF_SIZE / sizeof(uintptr_t), nb_cache_lines);
|
|
}
|
|
printf("[%p] Producer finished !\n", (void*) pthread_self());
|
|
/*
|
|
* When a producer end its thread-local storage vanished. Thus,
|
|
* producers must finish only after consumer has stopped using them
|
|
*/
|
|
pthread_mutex_lock(&mutex_cons_has_finished);
|
|
if (++producers_ended == nb_prod)
|
|
cont = 0;
|
|
if (!consumer_has_finished)
|
|
pthread_cond_wait(&cond_cons_has_finished, &mutex_cons_has_finished);
|
|
pthread_mutex_unlock(&mutex_cons_has_finished);
|
|
return NULL;
|
|
}
|
|
|
|
void onMessage(void *val)
|
|
{
|
|
//printf("Receive value: %p\n", (void *) val);
|
|
}
|
|
|
|
void *receptor(void *a)
|
|
{
|
|
if (shared)
|
|
{
|
|
pthread_t tid;
|
|
cpu_set_t cpuset;
|
|
|
|
tid = pthread_self();
|
|
CPU_ZERO(&cpuset);
|
|
CPU_SET(0, &cpuset);
|
|
if (pthread_setaffinity_np(tid, sizeof(cpu_set_t), &cpuset))
|
|
{
|
|
perror("pthread_setaffinity_np");
|
|
return NULL;
|
|
}
|
|
}
|
|
reception(onMessage);
|
|
pthread_mutex_lock(&mutex_cons_has_finished);
|
|
consumer_has_finished = 1;
|
|
pthread_cond_broadcast(&cond_cons_has_finished);
|
|
pthread_mutex_unlock(&mutex_cons_has_finished);
|
|
return NULL;
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int i;
|
|
void *return_value;
|
|
pthread_t *tid;
|
|
|
|
if (analyse_options(argc, argv))
|
|
return EXIT_FAILURE;
|
|
if (init_library())
|
|
return EXIT_FAILURE;
|
|
tid = (pthread_t *) malloc((nb_prod + 1) * sizeof(pthread_t));
|
|
if (tid == NULL)
|
|
{
|
|
fprintf(stderr, "Failed to allocate %lu bytes needed for thread creation\n", (nb_prod + 1) * sizeof(pthread_t));
|
|
return EXIT_FAILURE;
|
|
}
|
|
for(i = 0; i < nb_prod; i++)
|
|
pthread_create(&tid[i], NULL, producer, NULL);
|
|
pthread_create(&tid[i], NULL, receptor, NULL);
|
|
for(i = 0; i < nb_prod; i++)
|
|
pthread_join(tid[i], &return_value);
|
|
pthread_join(tid[i], &return_value);
|
|
free(tid);
|
|
return EXIT_SUCCESS;
|
|
}
|