Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / rst / rstzip3 / rstzip_v2 / mpsort.C
CommitLineData
920dae64
AT
1// ========== Copyright Header Begin ==========================================
2//
3// OpenSPARC T2 Processor File: mpsort.C
4// Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
6//
7// The above named program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public
9// License version 2 as published by the Free Software Foundation.
10//
11// The above named program is distributed in the hope that it will be
12// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public
17// License along with this work; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19//
20// ========== Copyright Header End ============================================
21#include <stdio.h>
22#include <string.h>
23
24#include "mpsort.H"
25
26#define NRECS (32000)
27#define NCPUS (4)
28
29int main(int argc, char* argv[]) {
30 FILE* infp = NULL;
31 FILE* outfp = stdout;
32 rstf_unionT* multirst = NULL;
33 RstSplit* unirst = NULL;
34 CpuCount* cpucount = NULL;
35 int index[NCPUS] = { 0 };
36 rstf_cpuT rstcpu = { CPU_T, 0, 0, 0, 0, 0 };
37 int curcpu = 0;
38 int firstcpurec = 1;
39 int i, j, count, nrecs, cpucountrecs;
40
41 // Quick check of command line.
42 if (argc != 2) {
43 fprintf(stderr, "mpsort rstfile\n");
44 exit(1);
45 }
46
47 // Open input file.
48 infp = fopen(argv[1], "r");
49
50 // Allocate buffers.
51 multirst = (rstf_unionT*) calloc(NRECS, sizeof(rstf_unionT));
52 unirst = (RstSplit*) calloc(NCPUS, sizeof(RstSplit));
53 for (i = 0; i < NCPUS; i++) {
54 unirst[i].rst = (rstf_unionT*) calloc(NRECS, sizeof(rstf_unionT));
55 }
56 cpucount = (CpuCount*) calloc(NRECS, sizeof(CpuCount));
57
58 nrecs = fread(multirst, sizeof(rstf_unionT), NRECS, infp);
59
60 while (nrecs > 0) {
61 for (i = 0; i < NCPUS; i++) {
62 unirst[i].nrecs = 0;
63 index[i] = 0;
64 }
65
66 cpucountrecs = sortRstTrace(multirst, nrecs, unirst, cpucount, &curcpu);
67 firstcpurec = 1;
68 j = 0;
69
70 for (i = 0; i < cpucountrecs; i++) {
71 curcpu = cpucount[i].cpu;
72 count = cpucount[i].count;
73
74 if (firstcpurec != 1) {
75 rstcpu.cpu = curcpu;
76 memcpy(&multirst[j], &rstcpu, sizeof(rstf_unionT));
77 j++;
78 }
79
80 memcpy(&multirst[j], &unirst[curcpu].rst[index[curcpu]],
81 count * sizeof(rstf_unionT));
82 index[curcpu] += count;
83 j += count;
84
85 firstcpurec = 0;
86 }
87
88 fwrite(multirst, sizeof(rstf_unionT), nrecs, outfp);
89 nrecs = fread(multirst, sizeof(rstf_unionT), NRECS, infp);
90 }
91
92 free(multirst);
93 for (i = 0; i < NCPUS; i++) {
94 free(unirst[i].rst);
95 }
96 free(unirst);
97 free(cpucount);
98}
99
100