Skip to content
Snippets Groups Projects
Commit 5f48d0f2 authored by Julien David's avatar Julien David
Browse files

Ajout benchmarks papi et découpage code

parent c6918dc6
No related branches found
No related tags found
No related merge requests found
Makefile 0 → 100644
CPPFLAGS= -O3 -g
all: benchmark benchmark_papi
benchmark: src/heap_sort.o src/random_generators.o src/benchmark.o
g++ $(CPPFLAGS) -o benchmark src/heap_sort.o src/random_generators.o src/benchmark.o
benchmark_papi: src/heap_sort.o src/random_generators.o src/benchmark_papi.o
g++ $(CPPFLAGS) -o benchmark_papi src/heap_sort.o src/random_generators.o src/benchmark_papi.o -lpapi
clean:
rm -f src/*.o
rm -f benchmark
rm -f benchmark_papi
4 826.104300 1.493700 8.537400 0.333700
8 1339.044300 6.517000 8.466900 0.914700
16 2747.364600 18.309500 8.488500 1.056300
32 6291.666100 41.830300 9.091200 1.392600
64 14785.317300 90.169600 10.920300 1.467000
128 34548.419800 189.315000 8.841800 1.410500
256 79574.465300 385.040100 12.294400 2.577800
512 180715.206400 786.368300 18.006700 3.818800
1024 404193.914200 1565.414100 32.214100 6.823000
2048 894344.597300 3119.601700 111.127600 26.521600
4096 1960881.343800 6221.691700 598.442600 105.627300
4 2.691000
8 11.625000
16 38.618000
32 110.312000
64 287.477000
128 709.014000
256 1682.503000
512 3888.728000
1024 8823.067000
4 2.691000 0.000001
8 11.625000 0.000000
16 38.618000 0.000001
32 110.312000 0.000001
64 287.477000 0.000003
128 709.014000 0.000005
256 1682.503000 0.000011
512 3888.728000 0.000024
1024 8823.067000 0.000051
2048 19718.027000 0.000109
4096 43566.646000 0.000235
8192 95355.968000 0.000506
16384 207141.919000 0.001095
32768 447120.472000 0.002393
65536 959816.092000 0.005340
#include<iostream>
#include<time.h>
#include"heap_sort.hpp"
#include"random_generators.hpp"
int main(int argc, char ** argv){
size_t max_size = 65536;
int permutation[max_size];
int size, xp;
int xp_num = 1000;
double mean;
double mean_time;
clock_t temps_deb, temps_fin;
for(size = 4; size <= max_size; size = 2*size){
mean = 0;
mean_time = 0;
for(xp = 0; xp < xp_num; xp++){
random_permutation(permutation, size);
temps_deb = clock();
mean +=heap_sort(permutation, size);
temps_fin = clock();
mean_time += (long double)(temps_fin - temps_deb)/(long double)CLOCKS_PER_SEC;
}
printf("%d %lf %lf\n", size, mean/xp_num, mean_time/xp_num);
}
return EXIT_SUCCESS;
}
#include<iostream>
#include<papi.h>
#include"heap_sort.hpp"
#include"random_generators.hpp"
#define PAPI_events_number 4
#define ERROR_RETURN(retval) { fprintf(stderr, "Error %d, %s, %s:line %d: \n", retval, PAPI_strerror(\
retval), __FILE__,__LINE__); exit(retval); }
int set_PAPI(){
int eventSet = PAPI_NULL;
int retval;
int events[PAPI_events_number] = {PAPI_TOT_INS, PAPI_BR_MSP, PAPI_L1_DCM, PAPI_L2_DCM};
int i;
/* We use number to keep track of the number of events in the eventSet */
if((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT )
ERROR_RETURN(retval);
/* Creating the eventset */
if ( (retval = PAPI_create_eventset(&eventSet)) != PAPI_OK)
ERROR_RETURN(retval);
/* Fill eventSet */
for (i = 0; i < PAPI_events_number; ++i){
if ( (retval = PAPI_add_event(eventSet, events[i])) != PAPI_OK)
ERROR_RETURN(retval);
}
return eventSet;
}
int main(int argc, char ** argv){
size_t max_size = 4096;
int permutation[max_size];
int size, xp;
int xp_num = 10000;
long long values[PAPI_events_number];
int retval;
long long int nb_instructions, nb_branch_fault;
long long int l1_misses, l2_misses, l3_misses;
int eventSet = set_PAPI();
for(size = 4; size <= max_size; size = 2*size){
nb_instructions = 0;
nb_branch_fault = 0;
l1_misses = l2_misses = l3_misses = 0;
for(xp = 0; xp < xp_num; xp++){
random_permutation(permutation, size);
if ( (retval = PAPI_start(eventSet)) != PAPI_OK)
ERROR_RETURN(retval);
heap_sort(permutation, size);
if ( (retval = PAPI_stop(eventSet, values)) != PAPI_OK)
ERROR_RETURN(retval);
nb_instructions += values[0];
nb_branch_fault += values[1];
l1_misses += values[2];
l2_misses += values[3];
//l3_misses += values[4];
}
printf("%d %lf %lf %lf %lf\n", size, nb_instructions/(double)xp_num, nb_branch_fault/(double)xp_num, l1_misses/(double)xp_num, l2_misses/(double)xp_num);
}
return EXIT_SUCCESS;
}
......@@ -39,7 +39,7 @@ public:
}
~BinaryHeap(){
delete this->data;
delete [] this->data;
}
bool isEmpty(){
......@@ -111,7 +111,10 @@ public:
while(min_child < (size-1) && heap_cmp(data[min_child], data[position])){
std::swap(data[min_child], data[position]);
position = min_child;
min_child = heap_cmp(data[left_child(position)],data[right_child(position)])? left_child(position): right_child(position);
if( right_child(position) < size-1 )
min_child = heap_cmp(data[left_child(position)],data[right_child(position)])? left_child(position): right_child(position);
else
min_child = size - 1;
counter_swap_down++;
}
}
......@@ -148,7 +151,7 @@ public:
};
std::ostream& operator<<(std::ostream& os, const BinaryHeap<int>& heap)
static std::ostream& operator<<(std::ostream& os, const BinaryHeap<int>& heap)
{
for( int i = 0; i < heap.size; i++)
os<<heap.data[i]<<" ";
......
#include<iostream>
#include"min_binary_heap.hpp"
#include"heap_sort.hpp"
/**
This function reallocates the Binary Heap only when necessary,
that is when the size the user asks for is greater than the current one.
@ensures size > 0
@param size is the maximal size of the binary heap asked by the user.
@return an allocated binary heap.
*/
MinBinaryHeap<int> * initialize_heap(size_t size){
static MinBinaryHeap<int> * mbh = NULL;
if(mbh == NULL)
......@@ -13,6 +20,15 @@ MinBinaryHeap<int> * initialize_heap(size_t size){
return mbh;
}
/**
Sorts the permutation the a binary heap
Time complexity: O(n log n)
Space complexity: O(n)
@param permutation is the permutation we want to sort
@param size is the size of the permutation
@return the number of swaps made during the sort.
*/
size_t heap_sort(int * permutation, size_t size){
int i;
MinBinaryHeap<int> * mbh = initialize_heap(size);
......@@ -23,30 +39,3 @@ size_t heap_sort(int * permutation, size_t size){
permutation[i] = mbh->extract();
return mbh->getSwapsCounters();
}
void random_permutation(int * permutation, size_t size){
int i;
for(i = 0; i < size; i++)
permutation[i] = i;
for(i = 0; i < size; i++)
std::swap(permutation[i], permutation[i+rand()%(size-i)]);
}
int main(int argc, char ** argv){
int permutation[1024];
int size, xp;
int xp_num = 1000;
double mean;
for(size = 4; size <= 1024; size = 2*size){
mean= 0;
for(xp = 0; xp < xp_num; xp++){
random_permutation(permutation, size);
mean +=heap_sort(permutation, size);
}
printf("%d %lf\n", size, mean/xp_num);
}
return EXIT_SUCCESS;
}
#ifndef __HEAP_SORT_HPP__
#define __HEAP_SORT_HPP__
#include"min_binary_heap.hpp"
/**
This function reallocates the Binary Heap only when necessary,
that is when the size the user asks for is greater than the current one.
@ensures size > 0
@param size is the maximal size of the binary heap asked by the user.
@return an allocated binary heap.
*/
MinBinaryHeap<int> * initialize_heap(size_t size);
/**
Sorts the permutation the a binary heap
Time complexity: O(n log n)
Space complexity: O(n)
@param permutation is the permutation we want to sort
@param size is the size of the permutation
@return the number of swaps made during the sort.
*/
size_t heap_sort(int * permutation, size_t size);
#endif
#include"random_generators.hpp"
/**
Creates a random permutation of size 'size'
@param permutation is the tabular in which the permutation will be stored
@param size is the size of the permutation
@ensures size > 0
*/
void random_permutation(int * permutation, size_t size){
int i;
for(i = 0; i < size; i++)
permutation[i] = i;
for(i = 0; i < size; i++)
std::swap(permutation[i], permutation[i+rand()%(size-i)]);
}
#ifndef __RANDOM_GENERATORS_HPP__
#define __RANDOM_GENERATORS_HPP__
#include<iostream>
/**
Creates a random permutation of size 'size'
@param permutation is the tabular in which the permutation will be stored
@param size is the size of the permutation
@ensures size > 0
*/
void random_permutation(int * permutation, size_t size);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment