How do I join the sets

1

I have the function broken to make Jaccard's distance, which is the intersection division of sets and the union. The intersection comes out but the union does not. What the threads do is that each thread takes 1/4 of the vectorA and compares it to the whole vectorB

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 4

typedef struct interval{
    double distanciaJ;
    double k,u;
    int ini;
    int fin;
} aux;

double* vectorA;
double* vectorB;
int sizeA=5,sizeB=6;

void *funcion(void *threadid)
{
    int ini1,fin1, itera,interseccion,uni;
    aux *inter;
    inter = ((aux *) (threadid));
    itera = inter->distanciaJ;
    ini1 = inter->ini;
    fin1 = inter->fin;
    interseccion=inter->k;
    uni=inter->u;

    int i=0,j=0;
    double interseccionN=0,unionN=0;

    for(i=ini1;i<fin1;i++){     
        for(int j=0;j<sizeB;j++)
            if(vectorA[i]==vectorB[j]){
                interseccionN++;    
            }
    }

    inter->k=interseccionN;
    unionN=p+sizeB; 
    inter->u=unionN;
    inter->distanciaJ=interseccionN/unionN;     

    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
    pthread_t threads[NUM_THREADS];
    int rc;
    long t;
    double distanciaTotal=0;
    int i,resto=sizeA%NUM_THREADS;
    vectorA=(double*)malloc(sizeof(double)*sizeA);
    vectorB=(double*)malloc(sizeof(double)*sizeB);

    for(i=0;i<sizeA;i++){
        vectorA[i]=i;
        printf("%.2f",vectorA[i]);
    }
    printf("\n");
    for(i=0;i<sizeB;i++){
        vectorB[i]=2*i;
        printf("%.2f",vectorB[i]);
    }
    printf("\n");
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    aux param[NUM_THREADS];
    for(t=0;t<NUM_THREADS;t++) {        
        param[t].distanciaJ = 0;
        param[t].k=0;
        param[t].u=0;
        param[t].ini= t*(sizeA/NUM_THREADS)+(t<resto?t:resto);
        param[t].fin=param[t].ini+(sizeA/NUM_THREADS)+(t<resto);
        //printf("Creating thread %ld\n", t);
        rc = pthread_create(&threads[t], NULL, funcion, (void *) &param[t]);

        if (rc) {
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
    }
    double aux22=0,aux23;
    for(t=0; t<NUM_THREADS; t++) {
        rc = pthread_join(threads[t],NULL);
        distanciaTotal+=param[t].distanciaJ;
        aux22+=param[t].k;
        aux23+=param[t].u;
        if (rc) {
            printf("ERROR; return code from pthread_join() is %d\n", rc);
            exit(-1);
        }

    }
    printf("La interseccion es %.2f\n",aux22);
    printf("La union es %.2f\n",aux23);


    printf("La distancia de Jaccard es %.2f\n",distanciaTotal); 

    pthread_exit(NULL);
}
    
asked by RAZVAN LISMANU 20.11.2017 в 12:47
source

0 answers