1 Nisan 2014 Salı

pthreads ile combinasyon hesabı yaptıran program.

Paralel programlamaya çalışırken yazdığım bir program. İşine yarayanlar olabilir. Kodlar aşağıdadır.


/*********************************************************************
    2009010206036
    Bilgisayar Mühendisliği
    4. Sınıf II. Öğretim
    Burak Gökalp
Editör, Ortam;
Visual Studio 2013
Windows 7 x64 SP1
C(n / r) = n! / (r! * (n-r)!)
n! thread1
r! thread2
(n-r)! thread3 olarak hesap ettiriyorum
**********************************************************************/
#include <iostream>
#include <conio.h>
#include <pthread.h>

using namespace std;

void* Faktoriyel(void* argSayi)
{
    unsigned short int sayi = *(unsigned short*)argSayi;
    unsigned long long sonuc = 1, sayac = 1;
    cout << "Thread calisiyor";
    if (sayi > 1)
    {
        do{
            sonuc = sonuc * (++sayac);
        } while (sayi > sayac);
        return (void*)sonuc;
    }
    else return (void*)sonuc;
}

int main(char* argv[], int argc)
{
    unsigned short int uiN, uiR, uiFark;
    uiN = uiR = 0;
    unsigned long long uFaktoriyelN = 0, uFaktoriyelR = 0, uFaktoriyelNR = 0;
    void* sonuc=NULL;
    pthread_t thread1, thread2, thread3;
    int hata;
    do{
        cout << "\nKombinasyonu alinacak sayiyi giriniz:"; cin >> uiN;
        cout << "\nKombinasyonu giriniz:"; cin >> uiR;
    } while ((uiR > uiN) && (uiR >= 0)); //r =< n VE r >= 0 olmalıdır combinasyonda

    cout << "\nHesaplama Basladi... Lutfen bekleyiniz";
    uiFark = uiN - uiR; //n-r farkı
    hata = pthread_create(&thread1, NULL, Faktoriyel, (void*)&uiN);
    if (hata) exit(-1);
    hata = pthread_create(&thread2, NULL, Faktoriyel, (void*)&uiR);
    if (hata) exit(-1);
    hata = pthread_create(&thread3, NULL, Faktoriyel, (void*)&uiFark);
    if (hata) exit(-1);
    pthread_join(thread1, &sonuc);            //Thread1 bitmesi bekleniyor
    uFaktoriyelN = (unsigned long long)sonuc;    //Thread1 den gelen sonuç alınıyor
    pthread_join(thread2, &sonuc);            //Thread2 bitmesi bekleniyor
    uFaktoriyelR = (unsigned long long)sonuc;    //Thread2 den gelen sonuç alınıyor
    pthread_join(thread3, &sonuc);            //Thread3 bitmesi bekleniyor
    uFaktoriyelNR = (unsigned long long)sonuc;    //Thread3 den gelen sonuç alınıyor
    unsigned long long islemSonucu = uFaktoriyelN / (uFaktoriyelR * uFaktoriyelNR);
    cout << "\n\tC(" << uiN << "/" << uiR << ")" << endl
        << "Kombinasyonunun sonucu = " << islemSonucu;
    _getch();
    return 0;
}
Ayrıca pthreads kütüphanesinin fonksiyonlarına çalışmak istiyorsanız size aşağıdaki kaynağı önerebilirim. Kolay gelsin; https://computing.llnl.gov/tutorials/pthreads/

Hiç yorum yok:

Yorum Gönder