Parallelisation with openmp getting worse

I’m trying to parallelise the Fuzzy unsupervised c-means algorithm with openmp and I have done it and the problem is when I use 16/32 threads it should give better results than 8/4 threads but the opposite happens and it get worser and I’m not using threads more than the cores and I know that’s due to race conditions but at this moment my brain stopped working lol
Parallelised code is :
””

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#include <time.h>

#define MAX_DATA_POINTS 10000
#define MAX_CLUSTER 100
#define MAX_DATA_DIMENSION 5

int num_data_points;
int num_clusters;
int num_dimensions;
double low_high[MAX_DATA_DIMENSION][2];
double degree_of_memb[MAX_DATA_POINTS][MAX_CLUSTER];
double epsilon;
double fuzziness;
double data_point[MAX_DATA_POINTS][MAX_DATA_DIMENSION];
double cluster_centre[MAX_CLUSTER][MAX_DATA_DIMENSION];
double norms[MAX_DATA_POINTS][MAX_CLUSTER]; // Precomputed norms

// Initialize data and membership matrix
int init(char *fname) {
    int i, j, r, rval;
    FILE *f;
    double s;
    if ((f = fopen(fname, "r")) == NULL) {
        printf("Failed to open input file.");
        return -1;
    }
    fscanf(f, "%d %d %d", &num_data_points, &num_clusters, &num_dimensions);
    if (num_clusters > MAX_CLUSTER || num_data_points > MAX_DATA_POINTS || num_dimensions > MAX_DATA_DIMENSION) {
        printf("Input data exceeds defined limits.n");
        fclose(f);
        exit(1);
    }

    fscanf(f, "%lf %lf", &fuzziness, &epsilon);
    if (fuzziness <= 1.0 || epsilon <= 0.0 || epsilon > 1.0) {
        printf("Invalid fuzziness or epsilon.n");
        fclose(f);
        exit(1);
    }

    // Initialize data points and their range
    for (i = 0; i < num_dimensions; i++) {
        low_high[i][0] = __DBL_MAX__;
        low_high[i][1] = -__DBL_MAX__;
    }

    for (i = 0; i < num_data_points; i++) {
        for (j = 0; j < num_dimensions; j++) {
            fscanf(f, "%lf", &data_point[i][j]);
            if (data_point[i][j] < low_high[j][0])
                low_high[j][0] = data_point[i][j];
            if (data_point[i][j] > low_high[j][1])
                low_high[j][1] = data_point[i][j];
        }
    }

    // Initialize membership matrix randomly
    for (i = 0; i < num_data_points; i++) {
        s = 0.0;
        r = 100;
        for (j = 1; j < num_clusters; j++) {
            rval = rand() % (r + 1);
            r -= rval;
            degree_of_memb[i][j] = rval / 100.0;
            s += degree_of_memb[i][j];
        }
        degree_of_memb[i][0] = 1.0 - s;
    }

    fclose(f);
    return 0;
}

// Precompute norms to avoid redundant calculations
void precompute_norms() {
    #pragma omp parallel for collapse(2)
    for (int i = 0; i < num_data_points; i++) {
        for (int j = 0; j < num_clusters; j++) {
            double sum = 0.0;
            for (int k = 0; k < num_dimensions; k++) {
                double diff = data_point[i][k] - cluster_centre[j][k];
                sum += diff * diff;
            }
            norms[i][j] = sqrt(sum);
        }
    }
}

// Calculate new cluster centers
int calculate_centre_vectors() {
    double t[MAX_DATA_POINTS][MAX_CLUSTER];

    #pragma omp parallel for collapse(2)
    for (int i = 0; i < num_data_points; i++) {
        for (int j = 0; j < num_clusters; j++) {
            t[i][j] = pow(degree_of_memb[i][j], fuzziness);
        }
    }

    #pragma omp parallel for collapse(2)
    for (int j = 0; j < num_clusters; j++) {
        for (int k = 0; k < num_dimensions; k++) {
            double numerator = 0.0;
            double denominator = 0.0;
            for (int i = 0; i < num_data_points; i++) {
                numerator += t[i][j] * data_point[i][k];
                denominator += t[i][j];
            }
            cluster_centre[j][k] = numerator / denominator;
        }
    }
    return 0;
}

// Update membership values
double update_degree_of_membership() {
    double max_diff = 0.0;

    // Precompute norms
    precompute_norms();

    #pragma omp parallel for reduction(max:max_diff) collapse(2)
    for (int i = 0; i < num_data_points; i++) {
        for (int j = 0; j < num_clusters; j++) {
            double sum = 0.0;
            double norm_ij = norms[i][j];
            for (int k = 0; k < num_clusters; k++) {
                sum += pow(norm_ij / norms[i][k], 2.0 / (fuzziness - 1));
            }
            double new_uij = 1.0 / sum;
            double diff = fabs(new_uij - degree_of_memb[i][j]);
            if (diff > max_diff) {
                max_diff = diff;
            }
            degree_of_memb[i][j] = new_uij;
        }
    }
    return max_diff;
}

// FCM clustering process
int fcm(char *fname) {
    double max_diff;
    if (init(fname) != 0) return -1;
    do {
        calculate_centre_vectors();
        max_diff = update_degree_of_membership();
    } while (max_diff > epsilon);
    return 0;
}

// Print membership matrix to a file or stdout
void print_membership_matrix(char *fname) {
    int i, j;
    FILE *f;
    if (fname == NULL)
        f = stdout;
    else if ((f = fopen(fname, "w")) == NULL) {
        printf("Cannot create output file.n");
        exit(1);
    }

    fprintf(f, "Membership matrix Parallel:n");
    for (i = 0; i < num_data_points; i++) {
        fprintf(f, "Data[%d]: ", i);
        for (j = 0; j < num_clusters; j++) {
            fprintf(f, "%lf ", degree_of_memb[i][j]);
        }
        fprintf(f, "n");
    }
    if (fname != NULL)
        fclose(f);
}

// Main function
int main(int argc, char **argv) {
    if (argc != 2) {
        printf("USAGE: fcm <input file>n");
        exit(1);
    }
    double start_time = omp_get_wtime();
    fcm(argv[1]);
    double end_time = omp_get_wtime();
    double execution_time = end_time - start_time;
    printf("Number of data points: %dn", num_data_points);
    printf("Number of clusters: %dn", num_clusters);
    printf("Number of data-point dimensions: %dn", num_dimensions);
    printf("Accuracy margin: %lfn", epsilon);
    print_membership_matrix("membership.matrix");
    printf("The program took %f seconds.n", execution_time);
    return 0;
}

””

tried to optimise the performance and it got worser when I increased the number of threads

New contributor

Moataz Sarhan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật