File: sort.cpp

package info (click to toggle)
ispc 1.28.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 97,620 kB
  • sloc: cpp: 77,067; python: 8,303; yacc: 3,337; lex: 1,126; ansic: 631; sh: 475; makefile: 17
file content (113 lines) | stat: -rw-r--r-- 2,660 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
  Copyright (c) 2013, Durham University
  Copyright (c) 2023, Intel Corporation

  SPDX-License-Identifier: BSD-3-Clause
*/

/* Author: Tomasz Koziara */

#include "../../common/timing.h"
#include "sort_ispc.h"
#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace ispc;

extern void sort_serial(int n, unsigned int code[], int order[]);

static void progressBar(const int x, const int n, const int width = 50) {
    assert(n > 1);
    assert(x >= 0 && x < n);
    assert(width > 10);
    const float f = static_cast<float>(x) / (n - 1);
    const int w = static_cast<int>(f * width);

    // print bar
    std::string bstr("[");
    for (int i = 0; i < width; i++)
        bstr += i < w ? '=' : ' ';
    bstr += "]";

    // print percentage
    std::stringstream pstr0;
    pstr0 << " " << static_cast<int>(f * 100.0) << " % ";
    const std::string pstr(pstr0.str());
    std::copy(pstr.begin(), pstr.end(), bstr.begin() + (width / 2 - 2));

    std::cout << bstr;
    std::cout << (x == n - 1 ? "\n" : "\r") << std::flush;
}

int main(int argc, char *argv[]) {
    int i, j, n = argc == 1 ? 1000000 : atoi(argv[1]), m = n < 100 ? 1 : 50, l = n < 100 ? n : RAND_MAX;
    double tISPC1 = 0.0, tISPC2 = 0.0, tSerial = 0.0;
    unsigned int *code = new unsigned int[n];
    int *order = new int[n];

    srand(0);

    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++)
            code[j] = rand() % l;

        reset_and_start_timer();

        sort_ispc(n, code, order, 1);

        tISPC1 += get_elapsed_mcycles();

        if (argc != 3)
            progressBar(i, m);
    }

    printf("[sort ispc]:\t[%.3f] million cycles\n", tISPC1);

    srand(0);

    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++)
            code[j] = rand() % l;

        reset_and_start_timer();

        sort_ispc(n, code, order, 0);

        tISPC2 += get_elapsed_mcycles();

        if (argc != 3)
            progressBar(i, m);
    }

    printf("[sort ispc + tasks]:\t[%.3f] million cycles\n", tISPC2);

    srand(0);

    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++)
            code[j] = rand() % l;

        reset_and_start_timer();

        sort_serial(n, code, order);

        tSerial += get_elapsed_mcycles();

        if (argc != 3)
            progressBar(i, m);
    }

    printf("[sort serial]:\t\t[%.3f] million cycles\n", tSerial);

    printf("\t\t\t\t(%.2fx speedup from ISPC, %.2fx speedup from ISPC + tasks)\n", tSerial / tISPC1, tSerial / tISPC2);

    delete[] code;
    delete[] order;
    return 0;
}