Commit 0eb57d44 authored by Alexandre Peres Arias's avatar Alexandre Peres Arias
Browse files

tudo no mesmo


Signed-off-by: Alexandre Peres Arias's avatarAlexandre Peres Arias <apa13@inf.ufpr.br>
parent 305b4904
Pipeline #6995 failed with stage
in 50 seconds
all:
gcc knn_hamming.c recomenda.c hamming.c -o ham -std=c99 -lm -Wall -Wno-unused-result -g -O3 -lpthread
gcc -pthread knn_hamming.c -o ham -std=c99 -lm -Wall -Wno-unused-result -g -O3
#TODO: ajeitar o makefile
#include "hamming.h"
#include "recomenda.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <float.h>
#include <pthread.h>
#include <unistd.h>
typedef struct hamm{
int ind;
float dis;
} hamming_str;
typedef struct n_it{
int sum;
int ind;
}n_itens;
int N_THREADS,K,DOM;
int LIN,COL,N;
int **dados, **recom;
hamming_str **hamm;
inline int* hamming(int **M, int *user);
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);
// 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 ){
int aval=0, dist=0, indx, min;
int *buf;
hamming_str *tmp_hamm;
//qntos items avaliados para esse usuario
for(int j=0; j<COL; ++j) aval+=user[j];
for(int i=0; i<LIN; ++i){
buf = M[i];
dist=0;
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;
for(int k=0; k<K; ++k){
///pega a menor distancia
if(min > tmp_hamm[k].dis){
min = tmp_hamm[k].dis;
indx = k;
}
}
if(dist > min){
tmp_hamm[indx].dis = dist;
tmp_hamm[indx].ind = i;
}
}
return(recomenda(user, tmp_hamm));
}
// ***********************************************************
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
for(int j=0; j<COL; ++j){
if(user[j]){
tot=0;
for(int k=0; k<K; ++k) tot+=dados[ham[k].ind][j];
if(tot > nit[0].sum){
nit[0].sum = tot; nit[0].ind = j;
//bota min na primeira
ordena_min(nit);
}
}
}
return ordena(nit);
}
inline void ordena_min(n_itens *nit){
int min=nit[0].sum, id = 0;
for(int i=1; i<K;++i)
if(min > nit[i].sum){
min = nit[i].sum;
id=i;
}
//troca min
if(id){
int tmp = nit[id].ind;
nit[id].sum = nit[0].sum;
nit[id].ind = nit[0].ind;
nit[0].sum=min;
nit[0].ind = tmp;
}
}
inline int *ordena(n_itens *nit){
int maior_ind;
int *nit_n;
nit_n = (int*) malloc(sizeof(int)*N);
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)
maior_ind = i;
nit_n[n] = nit[maior_ind].ind;
nit[maior_ind].sum = 0;
}
free(nit);
return nit_n;
}
/*-----------------------------------------------------------------------------
* Aloca as matrizes de teste e treino já guardando seus labels */
extern inline int **alloc_data(FILE *f, int h){
inline int **alloc_data(FILE *f, int h){
//retirei leitura do label e mudei float para int
//
int **data = (int**) malloc(sizeof(int*)*LIN);
......@@ -28,7 +159,7 @@ extern 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*/
extern inline void *calc_dist(void *in){
inline void *calc_dist(void *in){
int ind_th = *((int*)in);
int max = ind_th*DOM + DOM;
......
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