Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It is easy to schedule task on uniprocessor but what we do if we want to schedule task on multiprocessors with several sheduling criteria.

What I have tried:

int
main(int argc, char **argv)
{
    FILE *fp    = 0;
    int size    = 0;
    int cpus = 0;
    int iter    = 0;
    int *a      = 0;

    int *b      = 0;
    int *c      = 0;
    int bsize   = 0;
    int csize   = 0;
    int count   = 0;
    
    int sum     = 0;

    work *j     = 0;

    if(argc != 2)   {
        fprintf(stderr, "syntax:: r input_file\n");
        return 1;
    }

    fp = fopen(argv[1], "r");
    if(!fp) {
        fprintf(stderr, "error opening file [%s]\n", argv[1]);
        return 1;
    }
    // File format: size nr_cpus int1 int2 ... intsize
    fscanf(fp, "%d", &size);
    fscanf(fp, "%d", &cpus);
    if(!size || size < 0) {
        fprintf(stderr, "invalid size [%d]\n", size);
        fclose(fp);
        return 1;
    }
    if(!cpus || cpus < 0) {
        fprintf(stderr, "invalid cpus [%d]\n", cpus);
        fclose(fp);
        return 1;
    }
    fprintf(stdout, " size [%d] cpus [%d]\n", size, cpus);
    a  = (int *)calloc(1, size * sizeof(int));
    for(int i = 0; i < size; ++i) {
        fscanf(fp, "%d", &a[i]);
    }
    iter = _log2(cpus);
    if(iter == -1 || exp2(iter) != cpus) {
        fprintf(stderr, "invalid cpus not power of two cpus [%d] iter [%d]", cpus, iter);
        fclose(fp);
        return 1;
    }
    fprintf(stdout, " log2 [%d]\n", iter);
    work_q .push(new work(a, size, 0));
    for(int i = 0; i < iter; ++i) {
        j = work_q.front();
        if(!j) {
            break;
        }
        while(j && j->pass == i) {
            work_q.pop();
            p(j->a,j->size,&b,&bsize,&c,&csize);
            if(bsize)
                work_q.push(new work(b,bsize,j->pass+1));
            if(csize)
                work_q.push(new work(c,csize,j->pass+1));
            free((void *)j->a);
            delete j;
            j = work_q.front();
        }
    }
    // The final schedule is in the queue.
    fprintf(stdout, " queue size [%d]\n", work_q.size());
    size = work_q.size();
    for(count = 0; count < size; ++count) {
        printf("cpu = [%d]\n",count);
        j = work_q.front();
        sum = 0;
        printf(" len = [%d] { ", j->size);
        for(int k = 0; k < j->size; ++k) {
            printf("%d ", j->a[k]);
            sum += j->a[k];
        }
        printf("} \n");
        printf(" sum = [%d]\n", sum);
        free((void *)j->a);
        delete j;
        work_q.pop();
    }
    fclose(fp);
    return 0;
}
Posted
Updated 11-Apr-18 5:06am

1 solution

Scheduling on different processor is a task for the operating system and has also some performance costs like the scheduling process or data syncronization. Normally it makes most sense to identify the performance bottlenecks and optimize them. Threading makes sense, when waiting for slow resources like network data or database access.

This is a good starter tutorial on threading.

My tip: check at the end that the performance really improves.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900