add average thread

This commit is contained in:
quentin.perret 2022-06-14 10:24:59 +02:00
parent 6cd7513501
commit 05a9142c7e
10 changed files with 75 additions and 12 deletions

View file

@ -1,7 +1,7 @@
CC = gcc
all:
$(CC) fileGestion.c getArray.c power.c queue.c simulateFlux.c main.c -lm -lpthread -o main
$(CC) fileGestion.c getArray.c average.c power.c queue.c simulateFlux.c main.c -lm -lpthread -o main
# $(CC) queue.c simulateFlux.c main.c -lm -lpthread -o main
./main < ../02400031.TXT

32
Code-C/average.c Normal file
View file

@ -0,0 +1,32 @@
#include "average.h"
#include "getArray.h"
#include "fileGestion.h"
#include "initialParameters.h"
#include "queue.h"
#include <assert.h>
void averageCalculation(long **p, double averageArray[] , int N, int M){
for(int i = 0; i < M-1; i++){
int j = 0;
averageArray[i] = 0;
while(j < N){
averageArray[i] += p[i][j];
j++;
}
averageArray[i] /= N;
//printf("%f\n", powerArray[i]);
}
}
bool average(char* rawDataFileName,int N , int M){
long **p = getRawDataArray(rawDataFileName,N, M);
double aver[8];
if(p !=NULL){
averageCalculation(p,aver,N,M);
writeDataInFile("averageData.csv",aver,8);
freeArray(p,N);
return true;
}
else{
return false;
}
}

4
Code-C/average.h Normal file
View file

@ -0,0 +1,4 @@
#include <math.h>
#include <stdbool.h>
bool average(char* rawDataFileName,int N , int M);

View file

@ -26,8 +26,8 @@ void clearRawData(int nRow){
* @param powerArray
* @param nCol size of the power array, correspond to the number of captor used
*/
void writePowerData(double powerArray[], int nCol){
FILE *f = fopen("powerData.csv","a+");
void writeDataInFile(char* fileName , double powerArray[], int nCol){
FILE *f = fopen(fileName,"a+");
for(int i = 0 ; i < nCol ; i++){
if( i < nCol-1){
fprintf(f, "%f , ", powerArray[i]);

View file

@ -5,4 +5,4 @@
void clearRawData(int N);
void writePowerData(double a[], int N);
void writeDataInFile(char* fileName , double a[], int N);

Binary file not shown.

View file

@ -4,9 +4,10 @@
#include "power.h"
#include "initialParameters.h"
#include "queue.h"
#include "average.h"
bool rawDataWriteFlag;
int nRow = 100000;
int nRow = 500;
int nCol = 9;
double freqEch = 250;
@ -21,7 +22,7 @@ int cptFile = 1;
double period = 0;
double invTimeBandWidth = 0;
void *threadCalcul(void *vargp){
void *threadCalculPower(void *vargp){
Pqueue rawDataQueue = firstRawDataQueue;
char* fileName;
while(rawDataWriteFlag){
@ -29,6 +30,34 @@ void *threadCalcul(void *vargp){
rawDataQueue = queueGetNextE(rawDataQueue);
fileName = queueGetTabChar(rawDataQueue);
power(fileName,nRow,nCol,period,invTimeBandWidth);
remove(fileName);
}
}
}
void *threadCalculAverage(void *vargp){
Pqueue rawDataQueue = firstRawDataQueue;
char* fileName;
while(rawDataWriteFlag){
while(queueGetNextE(rawDataQueue) != NULL){
rawDataQueue = queueGetNextE(rawDataQueue);
fileName = queueGetTabChar(rawDataQueue);
average(fileName,nRow,nCol);
remove(fileName);
}
}
}
void *threadCalculBoth(void *vargp){
Pqueue rawDataQueue = firstRawDataQueue;
char* fileName;
while(rawDataWriteFlag){
while(queueGetNextE(rawDataQueue) != NULL){
rawDataQueue = queueGetNextE(rawDataQueue);
fileName = queueGetTabChar(rawDataQueue);
power(fileName,nRow,nCol,period,invTimeBandWidth);
average(fileName,nRow,nCol);
remove(fileName);
}
}
}
@ -45,7 +74,7 @@ int main(int argc , char** argv){
pthread_create(&rawData , NULL, simulateFlux, (void *)&rawData);
pthread_t calcul;
pthread_create(&calcul , NULL, threadCalcul, (void *)&calcul);
pthread_create(&calcul , NULL, threadCalculBoth, (void *)&calcul);
pthread_exit(NULL);
}

View file

@ -24,9 +24,8 @@ bool power(char* rawDataFileName,int N , int M, double periode , double invTimeB
double pw[8];
if(p !=NULL){
powerCalculation(p,pw,N,M,periode,invTimeBandwidth);
writePowerData(pw,8);
writeDataInFile("powerData.csv",pw,8);
freeArray(p,N);
remove(rawDataFileName);
return true;
}
else{

View file

@ -1,5 +1,4 @@
#include <math.h>
#include <stdbool.h>
bool power(char* rawDataFileName,int N , int M, double periode , double invTimeBandwidth);
void *threadCalcul(void *vargp);
bool power(char* rawDataFileName,int N , int M, double periode , double invTimeBandwidth);

View file

@ -105,7 +105,7 @@ bool writeOneRawData(FILE *rawDataFile){
}
}
cptData++;
//sleep(0.004); //simul la freq ech
sleep(0.004); //simul la freq ech
return true;
}
else {