• Rvalue Reference Declarator: va Move Constructors and Move Assignment Operators (C++) ga qarang.
  • Raqamli texnologiyalar vazirligi muhammad al-xorazmiy nomidagi toshkent axborot texnologiyalari universiteti qarshi filiali “kompyuter injiniringi” fakulteti




    Download 143,49 Kb.
    bet11/13
    Sana25.05.2024
    Hajmi143,49 Kb.
    #253828
    1   ...   5   6   7   8   9   10   11   12   13
    Bog'liq
    1-amaliy ish

    Maslahat
    Ushbu parallel tartiblash algoritmlari harakat semantikasini qo'llab-quvvatlaydi. O'zgartirish operatsiyalarini samaraliroq bajarish uchun siz ko'chirishni tayinlash operatorini belgilashingiz mumkin. Ko‘chirish semantikasi va ko‘chirishni tayinlash operatori haqida qo‘shimcha ma’lumot olish uchun Rvalue Reference Declarator: && va Move Constructors and Move Assignment Operators (C++) ga qarang. Agar ko'chirishni tayinlash operatori yoki almashtirish funktsiyasi taqdim etilmagan bo'lsa, tartiblash algoritmlari nusxa ko'chirish konstruktoridan foydalanadi.
    Quyidagi asosiy misol, turdagi qiymatlar parallel_sortvektorini saralash uchun algoritmdan foydalanishni ko'rsatadi . Odatiy bo'lib, qiymatlarni solishtirish uchun std::less dan foydalanadi .vectorintparallel_sort
    C++Nusxalash
    // basic-parallel-sort.cpp
    // compile with: /EHsc
    #include

    #include
    #include


    using namespace concurrency;
    using namespace std;


    int wmain()
    {
    // Create and sort a large vector of random values.
    vector values(25000000);
    generate(begin(values), end(values), mt19937(42));
    parallel_sort(begin(values), end(values));


    // Print a few values.
    wcout << "V(0) = " << values[0] << endl;
    wcout << "V(12500000) = " << values[12500000] << endl;
    wcout << "V(24999999) = " << values[24999999] << endl;
    }
    /* Output:
    V(0) = -2147483129
    V(12500000) = -427327
    V(24999999) = 2147483311
    */
    Quyidagi misolda moslashtirilgan taqqoslash funktsiyasini qanday amalga oshirish kerakligi ko'rsatilgan. U std::complex::real usulidan std::complex qiymatlarini o'sish tartibida tartiblash uchun foydalanadi .
    C++Nusxalash
    // For this example, ensure that you add the following #include directive:
    // #include


    // Create and sort a large vector of random values.
    vector> values(25000000);
    generate(begin(values), end(values), mt19937(42));
    parallel_sort(begin(values), end(values),
    [](const complex& left, const complex& right) {
    return left.real() < right.real();
    });


    // Print a few values.
    wcout << "V(0) = " << values[0] << endl;
    wcout << "V(12500000) = " << values[12500000] << endl;
    wcout << "V(24999999) = " << values[24999999] << endl;
    /* Output:
    V(0) = (383,0)
    V(12500000) = (2.1479e+009,0)
    V(24999999) = (4.29497e+009,0)
    */
    Quyidagi misolda xesh funktsiyasini qanday amalga oshirish ko'rsatilgan parallel_radixsort. Ushbu misol 3D nuqtalarni tartiblaydi. Ular mos yozuvlar nuqtasidan masofa bo'yicha tartiblangan.
    C++Nusxalash
    // parallel-sort-points.cpp
    // compile with: /EHsc
    #include

    #include
    #include


    using namespace concurrency;
    using namespace std;


    // Defines a 3-D point.
    struct Point
    {
    int X;
    int Y;
    int Z;
    };


    // Computes the Euclidean distance between two points.
    size_t euclidean_distance(const Point& p1, const Point& p2)
    {
    int dx = p1.X - p2.X;
    int dy = p1.Y - p2.Y;
    int dz = p1.Z - p2.Z;
    return static_cast(sqrt((dx*dx) + (dy*dy) + (dz*dz)));
    }


    int wmain()
    {
    // The central point of reference.
    const Point center = { 3, 2, 7 };


    // Create a few random Point values.
    vector
    values(7);
    mt19937 random(42);
    generate(begin(values), end(values), [&random] {
    Point p = { random()%10, random()%10, random()%10 };
    return p;
    });


    // Print the values before sorting them.
    wcout << "Before sorting:" << endl;
    for_each(begin(values), end(values), [center](const Point& p) {
    wcout << L'(' << p.X << L"," << p.Y << L"," << p.Z
    << L") D = " << euclidean_distance(p, center) << endl;
    });
    wcout << endl;


    // Sort the values based on their distances from the reference point.
    parallel_radixsort(begin(values), end(values),
    [center](const Point& p) -> size_t {
    return euclidean_distance(p, center);
    });


    // Print the values after sorting them.
    wcout << "After sorting:" << endl;
    for_each(begin(values), end(values), [center](const Point& p) {
    wcout << L'(' << p.X << L"," << p.Y << L"," << p.Z
    << L") D = " << euclidean_distance(p, center) << endl;
    });
    wcout << endl;
    }
    /* Output:
    Before sorting:
    (2,7,6) D = 5
    (4,6,5) D = 4
    (0,4,0) D = 7
    (3,8,4) D = 6
    (0,4,1) D = 7
    (2,5,5) D = 3
    (7,6,9) D = 6


    After sorting:
    (2,5,5) D = 3
    (4,6,5) D = 4
    (2,7,6) D = 5
    (3,8,4) D = 6
    (7,6,9) D = 6
    (0,4,0) D = 7
    (0,4,1) D = 7
    */
    Misol uchun, bu misol nisbatan kichik ma'lumotlar to'plamidan foydalanadi. Kattaroq ma'lumotlar to'plamlarida yaxshilangan ishlash bilan tajriba qilish uchun boshlang'ich vektor hajmini oshirishingiz mumkin.
    Ushbu misolda xesh funktsiyasi sifatida lambda ifodasi qo'llaniladi. Shuningdek, siz std:: hash sinfining o'rnatilgan ilovalaridan birini ishlatishingiz yoki o'zingizning mutaxassisligingizni belgilashingiz mumkin. Shu bilan bir qatorda, quyidagi misolda ko'rsatilganidek, maxsus xesh funktsiyasi obyektidan foydalanishingiz mumkin:



    Download 143,49 Kb.
    1   ...   5   6   7   8   9   10   11   12   13




    Download 143,49 Kb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Raqamli texnologiyalar vazirligi muhammad al-xorazmiy nomidagi toshkent axborot texnologiyalari universiteti qarshi filiali “kompyuter injiniringi” fakulteti

    Download 143,49 Kb.