ThreadsProcess - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

ThreadsProcess

mozalamozala Posts: 8Member
Can somebody help me out?
--------------------------------------
Input: Square matrix A of nxn integers, where n is even.

Rotate the elements of both diagonals in the clock direction.

Input matrix 4x4

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Matrix 4x4 after rotation:

13 2 3 1
5 10 6 8
9 11 7 12
16 14 15 4

Rotation is executed by 2n threads/processes.
One quarter n/2 of threads/processes can
read data from the upper half of the
main diagonal (e.g. 4,7) and write them
to the lower half of the secondary diagonal (e.g. 11,16).
The second quarter of threads/processes can read data from
the lower half of the secondary diagonal (e.g. 11,16) and
write them to the lower half of the main diagonal (e.g. 10,13).
The third quarter of threads/processes can read data from the
lower half of the main diagonal (e.g. 10,13) and write them to
the upper half of the secondary diagonal (e.g. 1,6). The last
quarter of threads/processes can read data from the upper half of
the secondary diagonal (e.g. 1,6) and write them to the upper
half of the main diagonal (e.g. 4,7).

Output: Input and output matrix.

-------------------------------
I've tried to begin the code so just pick out some mistakes and correct so that it can work..Thanks
----------------------------------
#include
#include
#include
#include
#include
#include
#define MAX_THREADS 100

int Elmnt[10000];
int **tmp;
int m, n, Elmntsize, t, k, kAbs;
pthread_mutex_t mutex;
pthread_cond_t cond[MAX_THREADS];

/////////////////////////////////////
void Print () {
int i;
for(i=0; i=iL; i--){
Elmnt[i+kAbs] = Elmnt[i];
}
if(id == t-1) {
tmpShift(id, iL);
pthread_cond_signal(&cond[0]);
}
else {
pthread_cond_wait(&cond[id], &mutex);
tmpShiftRR(id, iL);
pthread_cond_signal(&cond[id+1]);
}
}
---------------------------------------
// Left rotation function //
---------------------------------------
void RotationL(int id, int iL, int iR){
int i;
for (i=0; i m);
if(t>MAX_THREADS) t = MAX_THREADS;

// The number of positions to be rotated
printf("Enter number of positions to be rotated: ");
scanf("%d", &k);
kAbs = abs(k);
if(kAbs > ElmntSize-1 && k >= 0) kAbs = kAbs %ElmntSize; // oriznuti kroku na velikost pole
if(kAbs > ElmntSize && k < 0) kAbs = kAbs %ElmntSize;
kN = kAbs /n; // pocet presahu na dalsi radek
kAbs = kAbs %n; // total number of positions during the first pass
// alokovani pomocneho 2d pole pro pomocne presuny
tmp = (int **)malloc(t*sizeof(int*));
for(i=0; i<t; i++)
tmp[i]=(int *)malloc(n*sizeof(int));

// Created Thread
pthread_mutex_init(&mutex, NULL);
for (j=0; j<=kN; j++){
if (kAbs == 0) { kAbs = n; continue; }
for(i=0; i<t; i++) {
if(pthread_cond_init (&cond[i], NULL)) {
printf("ERROR:During pthread_cond_init() - number %d
)", i); exit(-1);
}
if(pthread_create(&ths[i], NULL, rotate, (void *) i)){
printf("ERROR: During thread_create() - number %d
)", i); exit(-1);
}
usleep(500000);
}
for(i=0; i<t; i++){
pthread_join(ths[i], NULL);
pthread_cond_destroy(&cond[i]);
}
kAbs = n;
}
pthread_mutex_destroy(&mutex);
free(tmp);
// Rotated array to be printed
printf("

Rotated Array:
");
Print();
printf("--- --- ---

");
sleep(2);
} while (1);
return 0;
}

Comments

  • bilderbikkelbilderbikkel Posts: 754Member
    : Can somebody help me out?
    Perhaps, when you'd have used code tags (i.e. [leftbr]code] and [leftbr]/code]).
    Perhaps until then,
    bilderbikkel

  • mozalamozala Posts: 8Member
    : : Can somebody help me out?
    : Perhaps, when you'd have used code tags (i.e. [leftbr]code] and [leftbr]/code]).
    : Perhaps until then,
    : bilderbikkel
    : Ok i'd like to find out how to interchange main and secondary diagonal or processing them by threads according to 2n->n/2->... like an exercise say above
    :[code]
    /////////////////////////////////////////////////////////////////////////
    // SHift of auxiliary tmp to new position(for right rotation) //
    void tmpShiftRR(int id, int iL){
    int i, preID;
    if(id == 0) preID = t-1;
    else preID = id-1;
    for (i=0; i=iL; i--){
    Elmnt[i+kAbs] = Elmnt[i];
    }
    if(id == t-1) {
    tmpShift(id, iL);
    pthread_cond_signal(&cond[0]);
    }
    else {
    pthread_cond_wait(&cond[id], &mutex);
    tmpShiftRR(id, iL);
    pthread_cond_signal(&cond[id+1]);
    }
    }
    ---------------------------------------
    // Left rotation function //
    ---------------------------------------
    void RotationL(int id, int iL, int iR){
    int i;
    for (i=0; i<kAbs; i++){ tmp[id][i] = Elmnt[iL+i]; }
    for (i=iL; i<=iR; i++){ Elmnt[i] = Elmnt[i+kAbs]; }
    if(id == t-1) {
    tmpShiftRL(id, iR);
    }
    else {
    pthread_cond_wait(&cond[id], &mutex);
    tmpShiftRL(id, iR);
    }
    if(id != 0) pthread_cond_signal(&cond[id-1]);
    }


    --------------------
    // rotate //
    --------------------
    void *rotate(void *threadid){
    int id, cntR, pocR, konR, iL, iR;
    id = (int)threadid;
    printf("The working Thread is number: %d
    ", id+1);
    cntR = avgCntRwTh(); // To find out how many rows to be worked on average amount
    pocR = (id)*cntR+1; // To provide rows where to begin rotation
    if(id == t-1) {
    cntR = m - (t-1)*cntR;
    sleep(1);
    } // For the Last thread of the remaining rows.
    konR = pocR+cntR -1;
    printf(" - This Threa is working with rows: %d-%d
    ", pocR, konR);
    //
    iL = (pocR-1)*n; //First element of respective Thread
    iR = konR*n -1; // Last element
    //
    pthread_mutex_lock(&mutex);
    //
    if(k < 0) rotationL(id, iL, iR); // left
    else rotationR(id, iL, iR); // right
    //
    pthread_mutex_unlock(&mutex);
    //
    pthread_exit(NULL);
    return NULL;
    }


    [/code]

  • bilderbikkelbilderbikkel Posts: 754Member
    : : : Can somebody help me out?
    : : Perhaps, when you'd have used code tags (i.e. [leftbr]code] and [leftbr]/code]).
    : : Perhaps until then,
    : : bilderbikkel
    : : Ok i'd like to find out how to interchange main and secondary diagonal or processing them by threads according to 2n->n/2->... like an exercise say above

    And a correct indentation of course!
    Instead of
    [code]
    void f()
    {
    for(int x=0; x<100;++x)
    {
    for(int y=0; y<100;++y)
    {
    for(int z=0; z<100;++z)
    {
    //Something
    }
    }
    }
    }
    [/code]
    use
    [code]
    void f()
    {
    for(int x=0; x<100;++x)
    {
    for(int y=0; y<100;++y)
    {
    for(int z=0; z<100;++z)
    {
    //Something
    }
    }
    }
    }
    [/code]
    It's not that I don't want to answer your question, I just don't want to do the indenting for you.
    See ya,
    bilderbikkel

  • mozalamozala Posts: 8Member
    : : : : Can somebody help me out?
    : : : Perhaps, when you'd have used code tags (i.e. [leftbr]code] and [leftbr]/code]).
    : : : Perhaps until then,
    : : : bilderbikkel
    : : : Ok i'd like to find out how to interchange main and secondary diagonal or processing them by threads according to 2n->n/2->... like an exercise say above
    :
    : And a correct indentation of course!
    : Instead of
    : [code]
    : void f()
    : {
    : for(int x=0; x<100;++x)
    : {
    : for(int y=0; y<100;++y)
    : {
    : for(int z=0; z<100;++z)
    : {
    : //Something
    : }
    : }
    : }
    : }
    : [/code]
    : use
    : [code]
    : void f()
    : {
    : for(int x=0; x<100;++x)
    : {
    : for(int y=0; y<100;++y)
    : {
    : for(int z=0; z<100;++z)
    : {
    : //Something
    : }
    : }
    : }
    : }
    : [/code]
    : It's not that I don't want to answer your question, I just don't want to do the indenting for you.
    : See ya,
    : bilderbikkel
    :
    :
    I hope this looks much better than b4,sorry for that ,at least now i know how to post a Thread next time.
    Thanks.
    Mozala
    [code]
    ----------------------------------------------------------------
    // SHift of auxiliary tmp to new position(for right rotation) //
    -----------------------------------------------------------------
    void tmpShiftRR(int id, int iL)

    {
    int i, preID;
    if(id == 0) preID = t-1;
    else preID = id-1;
    for (i=0; i<kAbs; i++)
    {
    Elmnt[iL+i]= tmp[preID][kAbs-1-i];
    }
    }
    ----------------------------------------
    // right rotation function //
    ----------------------------------------
    void RotationR(int id, int iL, int iR)

    {
    int i;
    for (i=0; i<kAbs; i++)
    {
    tmp[id][i] = Elmnt[iR-i];
    }
    for (i=iR-kAbs; i>=iL; i--)
    {
    Elmnt[i+kAbs] = Elmnt[i];
    }
    if(id == t-1)
    {
    tmpShift(id, iL);
    pthread_cond_signal(&cond[0]);
    }
    else
    {
    pthread_cond_wait(&cond[id], &mutex);
    tmpShiftRR(id, iL);
    pthread_cond_signal(&cond[id+1]);
    }
    }
    ---------------------------------------
    // Left rotation function //
    ---------------------------------------
    void RotationL(int id, int iL, int iR)

    {
    int i;
    for (i=0; i<kAbs; i++)
    {
    tmp[id][i] = Elmnt[iL+i];
    }
    for (i=iL; i<=iR; i++)
    {
    Elmnt[i] = Elmnt[i+kAbs];
    }
    if(id == t-1)
    {
    tmpShiftRL(id, iR);
    }
    else
    {
    pthread_cond_wait(&cond[id], &mutex);
    tmpShiftRL(id, iR);
    }
    if(id != 0) pthread_cond_signal(&cond[id-1]);
    }


    --------------------
    // rotate //
    --------------------
    void *rotate(void *threadid)

    {
    int id, cntR, pocR, konR, iL, iR;
    id = (int)threadid;
    printf("The working Thread is number: %d
    ", id+1);
    cntR = avgCntRwTh(); // To find out how many rows to be worked on average amount
    pocR = (id)*cntR+1; // To provide rows where to begin rotation
    if(id == t-1)
    {
    cntR = m - (t-1)*cntR;
    sleep(1);
    } // For the Last thread of the remaining rows.
    konR = pocR+cntR -1;
    printf(" - This Thread is working with rows: %d-%d
    ", pocR, konR);

    iL = (pocR-1)*n; //First element of respective Thread
    iR = konR*n -1; // Last element

    pthread_mutex_lock(&mutex);

    if(k < 0) rotationL(id, iL, iR); // left
    else rotationR(id, iL, iR); // right

    pthread_mutex_unlock(&mutex);

    pthread_exit(NULL);
    return NULL;
    }[/code]
Sign In or Register to comment.