fonctionne

This commit is contained in:
baillyj 2022-09-08 14:58:31 +02:00
parent 511bcbda0b
commit b67c4a8423
11 changed files with 132 additions and 0 deletions

BIN
a.out Executable file

Binary file not shown.

BIN
exo1 Executable file

Binary file not shown.

BIN
exo2 Executable file

Binary file not shown.

BIN
exo2.1 Executable file

Binary file not shown.

BIN
exo2.2 Executable file

Binary file not shown.

15
exo2.c Normal file
View File

@ -0,0 +1,15 @@
/* segment bss et data */
#define N 100000
//int t[N]; /* version 1 */
int t[N]={1}; /* version 2 */
int main()
{
return 0;
}
// 1) Le tableau se trouve dans les données car la variables est globale. La taille n'a pas changé malgrès le changement de variables globale.
// 2) La deuxième version est plus lourde.

BIN
exo3 Executable file

Binary file not shown.

BIN
exo3.1 Executable file

Binary file not shown.

31
exo3.c Normal file
View File

@ -0,0 +1,31 @@
/* accès mémoire */
#include<stdio.h>
#include<time.h>
#include <stdlib.h>
#define N 8192
int t[N][N];
static inline double tstamp(void)
{
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return tv.tv_sec + tv.tv_nsec * 1.0e-9;
}
int main()
{
int i,j;
double t1,t2;
t1=tstamp();
/* version 1 */ // for(i=0;i<N;i++) for(j=0;j<N;j++) t[i][j] = 1;
/* version 2 */ for(i=0;i<N;i++) for(j=0;j<N;j++) t[j][i] = 1;
t2=tstamp();
printf("time = %lf\n",t2-t1);
return 0;
}
// Le temps est différents entre les deux versions du programme, Pourquoi ?
// Overload de cache, la mémoire parcours le tableau en ligne.
// t[n][n]

BIN
exo4 Executable file

Binary file not shown.

86
exo4.c Normal file
View File

@ -0,0 +1,86 @@
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
static inline double tstamp(void)
{
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return tv.tv_sec + tv.tv_nsec * 1.0e-9;
}
void shuffle(int *array, size_t n)
{
if (n > 1)
{
size_t i;
// for (i = 0; i < n - 1; i++)
for (i = 0; i < n ; i++)
{
// size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
size_t j = rand()%n;
int t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}
void init_access_c(int access[],size_t size)
{
int i;
for(i=0;i<size;i++) access[i] = i;
}
void init_access_d(int access[],size_t size)
{
int i;
for(i=0;i<size;i++) access[i] = size-i-1;
}
void init_access_a(int access[],size_t size)
{
init_access_c(access,size);
shuffle(access,size);
}
void init_array(int t[],int N)
{
int i;
for(i=0;i<N;i++) t[i] = i ;
}
long int sum_array(int t[],int access[],size_t size)
{
long int S=0;
int i;
for(i=0;i<size;i++) S += t[access[i]];
return S;
}
int main(int argc,char * argv[])
{
double t1,t2;
int * array; // tableau à sommer (contient les tous les entiers [0,SIZE-1]
int * access;
int i,size;
long int S=0;
if (argc !=3) {
printf("%s -c|-d|-a SIZE\n",argv[0]);
return 1;
}
size=strtol(argv[2],NULL,0);
array=(int *)malloc(sizeof(int)*size);
assert(array != NULL);
access=(int *)malloc(sizeof(int)*size);
assert(access != NULL);
init_array(array,size);
if (strcmp(argv[1],"-c") == 0)
init_access_c(access,size);
if (strcmp(argv[1],"-d") == 0)
init_access_d(access,size);
if (strcmp(argv[1],"-a") == 0)
init_access_a(access,size);
/* On somme les elements en accedant au tableau
* sequentiellement (croissant/décroissant), ou
* de manière aléatoire
* */
t1=tstamp();
S= sum_array(array,access,size);
t2=tstamp();
printf("S=%ld %lf\n",S,(t2-t1));
}