Commit 5c86c374 authored by Alexandre Peres Arias's avatar Alexandre Peres Arias
Browse files

completo


Signed-off-by: Alexandre Peres Arias's avatarAlexandre Peres Arias <apa13@inf.ufpr.br>
parent 0eb57d44
Pipeline #6996 failed with stage
in 48 seconds
all:
gcc -pthread knn_hamming.c -o ham -std=c99 -lm -Wall -Wno-unused-result -g -O3
gcc -o ham knn_hamming.c -std=c99 -lm -Wall -Wno-unused-result -g -O3 -pthread
#TODO: ajeitar o makefile
This diff is collapsed.
#!/bin/bash
while read l; do
for i in `seq 1 1900`; do
vet[$i]=0
done
line=`echo $l | cut -d":" -f2 `
for pos in $line; do
vet[$pos]=1
done
echo ${vet[*]} | cut -d" " -f-1900 >> base1
done < perfs
for l in `seq 31 200`; do
for i in `seq 1 1900`; do
VET[$i]=0
done
QTDE=$(( ( RANDOM % 15 ) + 1))
while [[ QTDE -gt 0 ]]; do
POS=$(( ( RANDOM % 1900 ) + 1))
VET[$POS]=1
((QTDE--))
done
echo ${vet[*]} | cut -d" " -f-1900 >> base1
done
File added
5 8
0 0 0 1 0 1 1 1
0 0 0 0 0 1 1 0
1 0 0 1 0 1 0 0
0 0 1 0 1 0 1 0
1 1 1 1 1 1 1 1
......@@ -7,8 +7,8 @@
#include <unistd.h>
typedef struct hamm{
int ind;
float dis;
int ind;
int dis;
} hamming_str;
typedef struct n_it{
......@@ -21,33 +21,36 @@ int LIN,COL,N;
int **dados, **recom;
hamming_str **hamm;
inline int* hamming(int **M, int *user);
inline int* hamming(int **M, int user_id);
inline int* recomenda(int *user, hamming_str *ham);
inline void ordena_min(n_itens *nit);
inline int *ordena(n_itens *nit);
inline void *calc_dist(void *in);
extern inline void *calc_dist(void *in);
// MUDAR DE DIST EUCL PARA DIST COM COSSENOS OU MAHALANOBIS, MELHOR AINDA É DISTANCIA DE HAMMING!!!
// ESQUECER A BASE DE TREINO
/* ########################################################## */
inline int *hamming(int **M, int *user ){
inline int *hamming(int **M, int user_id ){
int aval=0, dist=0, indx, min;
int *buf;
hamming_str *tmp_hamm;
int *user = M[user_id];
//qntos items avaliados para esse usuario
for(int j=0; j<COL; ++j) aval+=user[j];
tmp_hamm = hamm[user_id];
min=INT_MAX; indx=0;
for(int i=0; i<LIN; ++i){
buf = M[i];
dist=0;
for(int j=0; j<COL; ++j) //fazer unroll desse laco
for(int j=0; j<COL; ++j){ //fazer unroll desse laco
dist += user[j] & buf[j];
tmp_hamm = hamm[i];
min=INT_MAX; indx=0;
//printf(" user_id %d j %d i %d dist %d\n", user_id, j,i, dist);
}
for(int k=0; k<K; ++k){
///pega a menor distancia
......@@ -59,8 +62,13 @@ inline int *hamming(int **M, int *user ){
if(dist > min){
tmp_hamm[indx].dis = dist;
tmp_hamm[indx].ind = i;
min = dist;
}
}
//printf("RECOMENDANDO USER %d \n",user_id);
//printf("HAM: ");
//for(int i=0; i<K;++i)
// printf(" usr %d, dis %d .",tmp_hamm[i].ind, tmp_hamm[i].dis);
return(recomenda(user, tmp_hamm));
}
......@@ -71,9 +79,12 @@ inline int *recomenda( int *user, hamming_str *ham){
int tot;
n_itens *nit; //num de interseccoes entre os likes, botar o min na prim posicao
nit = (n_itens*)malloc(sizeof(n_itens)*N); //N o num de recomendacoes
//inicia nit
for(int i=0; i<N; ++i) nit[i].sum = nit[i].ind=0;
for(int j=0; j<COL; ++j){
if(user[j]){
if(!user[j]){
tot=0;
for(int k=0; k<K; ++k) tot+=dados[ham[k].ind][j];
if(tot > nit[0].sum){
......@@ -83,14 +94,17 @@ inline int *recomenda( int *user, hamming_str *ham){
}
}
}
//printf("ordenando...\n");
//printf("antes de ordenar:");
//for(int i=0; i<N; ++i)
// printf("ind = %d sum = %d\n",nit[i].ind, nit[i].sum);
return ordena(nit);
}
inline void ordena_min(n_itens *nit){
int min=nit[0].sum, id = 0;
for(int i=1; i<K;++i)
for(int i=1; i<N;++i)
if(min > nit[i].sum){
min = nit[i].sum;
id=i;
......@@ -113,29 +127,24 @@ inline int *ordena(n_itens *nit){
int maior_ind;
int *nit_n;
nit_n = (int*) malloc(sizeof(int)*N);
maior_ind =0;
for(int n=0; n<N; ++n){
maior_ind = nit[0].ind;
for(int i=1; i<K; ++i)
if(nit[maior_ind].sum > nit[i].sum)
for(int i=0; i<N; ++i)
if(nit[maior_ind].sum < nit[i].sum)
maior_ind = i;
nit_n[n] = nit[maior_ind].ind;
nit[maior_ind].sum = 0;
}
free(nit);
//printf("recomendacao:");
//for(int i=0; i<N;++i)
// eprintf(" %d,\n", nit_n[i]);
return nit_n;
}
/*-----------------------------------------------------------------------------
* Aloca as matrizes de teste e treino já guardando seus labels */
......@@ -150,7 +159,7 @@ inline int **alloc_data(FILE *f, int h){
for(int i=0; i<LIN; ++i)
for(int j=0; j<COL; ++j)
fscanf(f, "%d", &data[i][j]);
return data;
}
......@@ -159,7 +168,7 @@ inline int **alloc_data(FILE *f, int h){
* Função principal da thread. Invoca e controla o processo. Cada thread tem um
* domínio para calcular e salvar as distâncias*/
inline void *calc_dist(void *in){
extern inline void *calc_dist(void *in){
int ind_th = *((int*)in);
int max = ind_th*DOM + DOM;
......@@ -168,7 +177,7 @@ inline void *calc_dist(void *in){
printf("***** THREAD %d domini = [%d,%d]\n",ind_th, ind_th*DOM,(max-1));
//salva vetor de recomendacao de cada usuario
for(int i=(ind_th*DOM); i<max; ++i)
recom[i] = hamming(dados, dados[i]);
recom[i] = hamming(dados, i);
return NULL;
}
......@@ -177,7 +186,7 @@ inline void *calc_dist(void *in){
int main(int argc, char *argv[]){
if(argc < 4){puts("ERRO ENTRADA");exit(1);}
if(argc != 5){puts("ERRO ENTRADA");exit(1);}
FILE *f1 = fopen(argv[1],"r");
K = atoi(argv[2]); //num de vizinhos para comparação
......@@ -188,16 +197,18 @@ int main(int argc, char *argv[]){
if( fscanf(f1,"%d",&COL) == EOF ) {puts("ERRO DE LEITURA 2"); exit(1);}
DOM = LIN/N_THREADS;
printf("COLUNAS = %d, LINHAS = %d, K = %d, NTHREAD = %d, N = %d\n",COL, LIN, K, N_THREADS, N);
hamm = (hamming_str**) malloc(sizeof(hamming_str*)*LIN);
for(int i=0;i<K;++i) hamm[i]=(hamming_str*)malloc(sizeof(hamming_str)*K);
for(int i=0;i<LIN;++i) hamm[i]=(hamming_str*)malloc(sizeof(hamming_str)*K);
recom = (int**) malloc(sizeof(int*)*LIN);
for(int i=0;i<LIN;++i) recom[i]=(int*)malloc(sizeof(int)*N);
for(int i=0; i<LIN; ++i)
for(int j=0; j<K; ++j) hamm[i][j].dis = 0;
puts(" AAAA");
printf("\n***** Dominio Per Thread = %d \n",DOM);
dados = alloc_data(f1,LIN);
puts("***** Base Alocado");
......@@ -206,6 +217,7 @@ int main(int argc, char *argv[]){
fclose(f1);
puts("***** PROCESSANDO...");
// THREADS
pthread_t *threads = (pthread_t*) malloc(sizeof(pthread_t)*N_THREADS);
int k[N_THREADS];
......@@ -214,11 +226,19 @@ int main(int argc, char *argv[]){
pthread_create(&(threads[i]), NULL, calc_dist, &k[i]);
}
sleep(1);
puts("***** PROCESSANDO...");
for(int i=0; i<N_THREADS; ++i)
pthread_join(threads[i], NULL);
puts("**** RECOMENDACOES ****");
for(int i=0; i<LIN; ++i){
printf(" ---- USUARIO %d:", i);
for(int j=0; j<N; ++j)
printf(" %d",recom[i][j]);
puts("");
}
puts("");
puts("***** TERMINOU PROCESSAMENTO!");
......
QUI : 8 3 16 214 218 600 1282 1755 1756 1854 1856 715 1000 1003 1008
BIO : 4 7 11 12 13 22 28 209 213 214 215 224 703 706 228 610 624 1011 1016 1017 1091 1126
MAT : 5 6 9 10 23 204 210 217 219 224 1203 1250 1313 1380 1744 1785 1895 701 702 704 1002 1004 1005 1095 1116 1121
+0
QUI_1 : 8 3 16 240 1282 1755
QUI_2 : 8 3 1854 1003 16 218 600
QUI_3 : 600 1755 1756 3 8 1000 1008
QUI_4 : 1003 715 218 1744 701 3 214 5
QUI_5 : 16 1282 1756 715 1003 1008 8 3 600
QUI_6 : 218 214 1754 715 8 3 16 600
QUI_7 : 600 3 218 1854 1000 1003 1008 8
QUI_8 : 16 214 600 1755 1854 715 1008
QUI_9 : 8 3 1008 214 1856 715 1003 1000
QUI_10 : 214 600 1755 1756 1854 715
+100
BIO_1 : 8 4 7 11 213 215 706
BIO_2 : 1126 12 13 4 209 213 214 22 28
BIO_3 : 709 706 228 11 12 13 7 1126 1011 628
BIO_4 : 703 1011 211 209 28 22
BIO_5 : 11 7 4 1016 1017 28 22
BIO_6 : 4 28 214 703 610 1011 1017 1126
BIO_7 : 1091 1011 1005 5 28 12 4
BIO_8 : 600 28 1250 228 11 213 1017 1005 4 7
BIO_9 : 22 209 214 224 703 228 1000 701 1016 1121 7 4 11
BIO_10 : 8 5 7 11 13 28 213 215 228 1011
+150
MAT_1 : 5 9 23 210 219 224 1203 1313 1380 1785 701 704 1004 1095
MAT_2 : 1121 1095 1004 5 9 10 4 204
MAT_3 : 9 10 23 204 210 217 250 1313 1380 701
MAT_4 : 1005 1116 1121 5 6 10 210 219 224 23
MAT_5 : 6 10 205 217 224 1380 7 2 1002 11
MAT_6 : 701 1250 217 23 1121 1116 1005 701 23 9 10
MAT_7 : 5 6 204 210 224 1203 1744 1895 1002 1004 1005
MAT_6 : 1121 1116 5 6 9 1203 1250 1313 1744 1895 23 704
MAT_7 : 5 6 9 224 715 600 1744 1785 701 1002 1004
MAT_8 : 219 217 210 610 1785 1744 2005 204 1121
MAT_9 : 6 9 10 5 23 204 219 1203 1313 1116 1121 210
MAT_10 : 6 9 10 23 217 224 1203 1744 1895 704 1002 1004 1005 1121
1: 8 3 16 240 1282 1755
2: 8 3 1854 1003 16 218 600
3: 600 1755 1756 3 8 1000 1008
4: 1003 715 218 1744 701 3 214 5
5: 16 1282 1756 715 1003 1008 8 3 600
6: 218 214 1754 715 8 3 16 600
7: 600 3 218 1854 1000 1003 1008 8
8: 16 214 600 1755 1854 715 1008
9: 8 3 1008 214 1856 715 1003 1000
10: 214 600 1755 1756 1854 715
11: 8 4 7 11 213 215 706
12: 1126 12 13 4 209 213 214 22 28
13: 709 706 228 11 12 13 7 1126 1011 628
14: 703 1011 211 209 28 22
15: 11 7 4 1016 1017 28 22
16: 4 28 214 703 610 1011 1017 1126
17: 1091 1011 1005 5 28 12 4
18: 600 28 1250 228 11 213 1017 1005 4 7
19: 22 209 214 224 703 228 1000 701 1016 1121 7 4 11
20: 8 5 7 11 13 28 213 215 228 1011
21: 5 9 23 210 219 224 1203 1313 1380 1785 701 704 1004 1095
22: 1121 1095 1004 5 9 10 4 204
23: 9 10 23 204 210 217 250 1313 1380 701
24: 1005 1116 1121 5 6 10 210 219 224 23
25: 6 10 205 217 224 1380 7 2 1002 11
26: 1121 1116 5 6 9 1203 1250 1313 1744 1895 23 704
27: 5 6 9 224 715 600 1744 1785 701 1002 1004
28: 219 217 210 610 1785 1744 2005 204 1121
29: 6 9 10 5 23 204 219 1203 1313 1116 1121 210
30: 6 9 10 23 217 224 1203 1744 1895 704 1002 1004 1005 1121
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment