153 lines
4.2 KiB
C
153 lines
4.2 KiB
C
#include <inttypes.h>
|
|
#include <pthread.h>
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/time.h>
|
|
#include <sys/types.h>
|
|
|
|
#include "discrete.h"
|
|
#include "private.h"
|
|
#include "pthread.h"
|
|
|
|
/* Create a thread that will run generator(prime). Use pthread_join() to
|
|
retrieve the result from the thread and return that value. */
|
|
uint64_t
|
|
find_gen (uint64_t prime)
|
|
{
|
|
pthread_t thread;
|
|
int rc = pthread_create (&thread, NULL, generator, (void *)prime);
|
|
if (rc != 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
void *result = NULL;
|
|
rc = pthread_join (thread, &result);
|
|
if (rc != 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return (uint64_t)result;
|
|
}
|
|
|
|
/* Helper function that is a wrapper for calling find_generator() in a
|
|
separate thread. Do not modify. */
|
|
void *
|
|
generator (void *args)
|
|
{
|
|
uint64_t prime = (uint64_t)args;
|
|
uint64_t gen = find_generator (prime);
|
|
pthread_exit ((void *)gen);
|
|
}
|
|
|
|
/* Create multiple threads, each of which makes a single call to generator().
|
|
Return a dynamically allocated array structured like the following:
|
|
results[0] = generator (0);
|
|
results[1] = generator (1);
|
|
...
|
|
The arguments are the number of threads to create and the array of primes
|
|
to distribute between the threads (one prime per thread). */
|
|
uint64_t *
|
|
find_gens (size_t num_threads, uint64_t *primes)
|
|
{
|
|
if (num_threads == 0 || primes == NULL)
|
|
return NULL;
|
|
|
|
pthread_t *threads = calloc (num_threads, sizeof (pthread_t));
|
|
uint64_t *results = calloc (num_threads, sizeof (uint64_t));
|
|
|
|
for (size_t i = 0; i < num_threads; i++)
|
|
{
|
|
int rc
|
|
= pthread_create (&threads[i], NULL, generator, (void *)primes[i]);
|
|
if (rc != 0)
|
|
{
|
|
for (size_t j = 0; j < i; j++)
|
|
{
|
|
pthread_join (threads[j], NULL);
|
|
}
|
|
free (threads);
|
|
free (results);
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
for (size_t i = 0; i < num_threads; i++)
|
|
{
|
|
void *retval = NULL;
|
|
int rc = pthread_join (threads[i], &retval);
|
|
if (rc != 0)
|
|
{
|
|
results[i] = 0;
|
|
}
|
|
else
|
|
{
|
|
results[i] = (uint64_t)retval;
|
|
}
|
|
}
|
|
|
|
free (threads);
|
|
return results;
|
|
}
|
|
|
|
/* Helper function to calculate the difference between a start and end time. */
|
|
double
|
|
time_diff (struct timeval start, struct timeval end)
|
|
{
|
|
double ending = end.tv_sec + (end.tv_usec * 0.000001);
|
|
double starting = start.tv_sec + (start.tv_usec * 0.000001);
|
|
return ending - starting;
|
|
}
|
|
|
|
/* Calculate the discrete logarithm for several values. That is, given the
|
|
values g, p, and g^n mod p, determine n. (Note that this is an intentionally
|
|
SLOW operation!) The parameters are stored in three global arrays:
|
|
gens[] - this contains the generator values (g)
|
|
primes[] - this contains the prime numbers (p)
|
|
mod_powers[] - this contains the values g^n mod p for unknown n values
|
|
In pseudocode, you should be doing:
|
|
for i in start_index .. end_index-1
|
|
logs [i] = discrete_log (parameters[i])
|
|
The return value is the real time it takes to compute these values. Use
|
|
the C standard library function gettimeofday() to get a start and end time,
|
|
then use the helper function time_diff() to compute the difference.
|
|
This function will be called by time_log_thread() below. */
|
|
double
|
|
time_log (size_t start_index, size_t end_index, uint64_t *gens,
|
|
uint64_t *primes, uint64_t *mod_powers, uint64_t *logs)
|
|
{
|
|
struct timeval start, end;
|
|
gettimeofday (&start, NULL);
|
|
|
|
for (size_t i = start_index; i < end_index; i++)
|
|
{
|
|
logs[i] = discrete_log (mod_powers[i], gens[i], primes[i]);
|
|
}
|
|
|
|
gettimeofday (&end, NULL);
|
|
return time_diff (start, end);
|
|
}
|
|
|
|
/* Wrapper function to call time_log() from within a thread. Given the
|
|
arguments passed, store the return value from time_log() into the
|
|
time_taken field. */
|
|
void *
|
|
time_log_thread (void *_args)
|
|
{
|
|
struct time_args *args = (struct time_args *)_args;
|
|
if (args == NULL)
|
|
pthread_exit (NULL);
|
|
|
|
size_t start = args->start_index;
|
|
size_t end = start + args->number;
|
|
|
|
args->time_taken = time_log (start, end, args->generators, args->primes,
|
|
args->mod_powers, args->results);
|
|
|
|
pthread_exit (NULL);
|
|
}
|