static void rwlock_test(pthread_t *p, int d, uint64_t *latency, void *(*f)(void *), const char *label) { int t; ck_pr_store_int(&barrier, 0); ck_pr_store_uint(&flag, 0); affinity.delta = d; affinity.request = 0; fprintf(stderr, "Creating threads (%s)...", label); for (t = 0; t < threads; t++) { if (pthread_create(&p[t], NULL, f, latency + t) != 0) { ck_error("ERROR: Could not create thread %d\n", t); } } fprintf(stderr, "done\n"); common_sleep(10); ck_pr_store_uint(&flag, 1); fprintf(stderr, "Waiting for threads to finish acquisition regression..."); for (t = 0; t < threads; t++) pthread_join(p[t], NULL); fprintf(stderr, "done\n\n"); for (t = 1; t <= threads; t++) printf("%10u %20" PRIu64 "\n", t, latency[t - 1]); fprintf(stderr, "\n"); return; }
struct task_desc *execute_task(struct task_desc *task) { switch (task->task_type) { case 0: /* STUB */ break; case 1: common_sleep(task->params); break; case 2: matrix_zero(task->params); break; case 3: naive_matrix_multiplication(task->params); break; case 4: optimized_matrix_multiplication(task->params); break; case 5: blocking_matrix_multiplication(task->params); break; case 6: parallel_matrix_multiplication(task->num_threads, task->params); break; default: /* Error not supported! */ break; } return task; }
void * common_timer_thread(void *param) { common_timer_t *timer; unsigned int elapsed; int sleepTime; timer = (common_timer_t *)param; while (timer->run) { // dorme o tempo setado menos o tempo que passou desde a última vez // que dormiu elapsed = getTimestamp() - timer->timeLast; // tempo passado desde o último sleep //printf("timer: time %d, elapsed %d \n", timer->time, elapsed); sleepTime = timer->time - elapsed; while (sleepTime > timer->interval) { if (!timer->run) break; common_sleep(timer->interval); sleepTime -= timer->interval; } if (!timer->run) break; common_sleep(sleepTime); timer->timeLast = getTimestamp(); // pra garantir que a thread está rodando if (!timer->run) break; // chama a função setada no timer if (timer->callback) { // se a função retorna algo != E_OK, para de executar o timer if (timer->callback(timer->callbackParam) != E_OK) { break; } } // se é timer para ser executado só uma vez, finaliza a thread if (timer->type == COMMON_TIMER_TYPE_ONCE) { break; } } timer->run = false; return NULL; }
int main(){ FILE * arquivo = NULL; long lSize; char * buffer; char caminho[256]; char ip[18]; int porta=0; queue_t * queue; netsend_struct_t * sender; int retorno; queue = queue_create(); sender = netsend_init(queue,NET_CONTENT_TYPE_VIDEO); netsend_packetSize(sender,1024,128); printf("CUIDADO - PROGRAMA CARREGA TODO O ARQUIVO PARA A MEMÓRIA RAM ANTES DE ENVIAR\n\n\n"); while(arquivo == NULL){ printf("Digite o caminho do arquivo a ser transferido para o servidor:\n"); scanf("%s",caminho); arquivo = fopen(caminho, "rb"); if(!arquivo) printf("Digite um caminho válido para o arquivo!\n\n"); } //tamanho fseek (arquivo, 0 , SEEK_END); lSize = ftell(arquivo); rewind (arquivo); buffer = (char *)queue_malloc(sizeof(char)*lSize); if(!buffer) printf("Seu sistema não possui memória livre sufiente para carregar o arquivo para memória!"); fread (buffer,1,lSize,arquivo); printf("Digite o IP do servidor:\n"); scanf("%s",ip); printf("Digite a porta do servidor:\n"); scanf("%d",&porta); netsend_setDest(sender, ip, porta); netsend_start(sender); retorno = queue_enqueue(queue,(uint8_t *)buffer,lSize,getTimestamp(),NULL); while(queue_length(queue)>0){ common_sleep(1000); }; netsend_stop(sender); netsend_end(sender); queue_appFinish(); printf("Arquivo enviado com sucesso!\n"); getchar(); return 0; };
int main(int argc, char *argv[]) { uint64_t v, d; unsigned int i; pthread_t *threads; struct block *context; ck_spinlock_t *local_lock; if (argc != 5) { ck_error("Usage: ck_cohort <number of cohorts> <threads per cohort> " "<affinity delta> <critical section>\n"); } n_cohorts = atoi(argv[1]); if (n_cohorts <= 0) { ck_error("ERROR: Number of cohorts must be greater than 0\n"); } nthr = n_cohorts * atoi(argv[2]); if (nthr <= 0) { ck_error("ERROR: Number of threads must be greater than 0\n"); } critical = atoi(argv[4]); if (critical < 0) { ck_error("ERROR: critical section cannot be negative\n"); } threads = malloc(sizeof(pthread_t) * nthr); if (threads == NULL) { ck_error("ERROR: Could not allocate thread structures\n"); } cohorts = malloc(sizeof(struct cohort_record) * n_cohorts); if (cohorts == NULL) { ck_error("ERROR: Could not allocate cohort structures\n"); } context = malloc(sizeof(struct block) * nthr); if (context == NULL) { ck_error("ERROR: Could not allocate thread contexts\n"); } a.delta = atoi(argv[2]); a.request = 0; count = malloc(sizeof(*count) * nthr); if (count == NULL) { ck_error("ERROR: Could not create acquisition buffer\n"); } memset(count, 0, sizeof(*count) * nthr); fprintf(stderr, "Creating cohorts..."); for (i = 0 ; i < n_cohorts ; i++) { local_lock = malloc(max(CK_MD_CACHELINE, sizeof(ck_spinlock_t))); if (local_lock == NULL) { ck_error("ERROR: Could not allocate local lock\n"); } CK_COHORT_INIT(basic, &((cohorts + i)->cohort), &global_lock, local_lock, CK_COHORT_DEFAULT_LOCAL_PASS_LIMIT); local_lock = NULL; } fprintf(stderr, "done\n"); fprintf(stderr, "Creating threads (fairness)..."); for (i = 0; i < nthr; i++) { context[i].tid = i; if (pthread_create(&threads[i], NULL, fairness, context + i)) { ck_error("ERROR: Could not create thread %d\n", i); } } fprintf(stderr, "done\n"); ck_pr_store_uint(&ready, 1); common_sleep(10); ck_pr_store_uint(&ready, 0); fprintf(stderr, "Waiting for threads to finish acquisition regression..."); for (i = 0; i < nthr; i++) pthread_join(threads[i], NULL); fprintf(stderr, "done\n\n"); for (i = 0, v = 0; i < nthr; i++) { printf("%d %15" PRIu64 "\n", i, count[i].value); v += count[i].value; } printf("\n# total : %15" PRIu64 "\n", v); printf("# throughput : %15" PRIu64 " a/s\n", (v /= nthr) / 10); for (i = 0, d = 0; i < nthr; i++) d += (count[i].value - v) * (count[i].value - v); printf("# average : %15" PRIu64 "\n", v); printf("# deviation : %.2f (%.2f%%)\n\n", sqrt(d / nthr), (sqrt(d / nthr) / v) * 100.00); return 0; }