forked from RoyalCaliber/vertexAPI2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.cuh
161 lines (132 loc) · 3.92 KB
/
util.cuh
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/******************************************************************************
Copyright 2013 Royal Caliber LLC. (http://www.royal-caliber.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
#ifndef UTIL_H__
#define UTIL_H__
#include <stdint.h>
#include <vector>
#include <algorithm>
#include <stdio.h>
//some simple utility routines to avoid duplication across test programs
//Return the current time in microseconds from epoch.
int64_t currentTime();
//extract arguments from the command line, useful for very simple
//command lines
//The format string is a sequence of one letter characters:
//s: string argument (returned via strdup, to be freed with free)
//i: integer
//f: float
//-x: a dash indicates the presence of an optional argument '-x'
// anywhere in the command line. The corresponding bool* argument
// is set to 1 if the option is present
//|: all positional arguments after the pipe symbol are optional
int parseCmdLineSimple(int argc, char **argv, const char*fmt, ...);
//Helper function for edgeListToCS*()
//this is a quick and dirty implementation because we are no concerned at this
//point with how a CSC or CSR representation is obtained.
//Return sortedIdx such that inData[sortedIdx] is sorted ascending
template<typename Int>
void indSort(int n, const Int* inData, Int* sortedIdx)
{
typedef std::pair<Int, Int> Pair;
struct PairCmp
{
static bool lt(const Pair &p1, const Pair &p2)
{
return p1.first < p2.first;
}
};
std::vector<Pair> pairs(n);
for( Int i = 0; i < n; ++i )
{
pairs[i].first = inData[i];
pairs[i].second = i;
}
std::sort(pairs.begin(), pairs.end(), PairCmp::lt);
for( Int i = 0; i < n; ++i )
sortedIdx[i] = pairs[i].second;
}
//convert a list of edges into a CSR representation of the adjacency matrix
//again, this is not intended to be efficient, since this is outside the
//scope of the present work.
//Note: offsets should have nVertices + 1 elements.
template<typename Int>
void edgeListToCSR(Int nVertices, Int nEdges
, const Int *srcs, const Int *dsts
, Int *offsets, Int *outDsts, Int* sortIndices)
{
Int* ind;
std::vector<Int> tmpIndices;
if( sortIndices )
ind = sortIndices;
else
{
tmpIndices.resize(nEdges);
ind = &tmpIndices[0];
}
indSort(nEdges, srcs, ind);
if( outDsts )
{
for( Int i = 0; i < nEdges; ++i )
outDsts[i] = dsts[ ind[i] ];
}
Int curSrc = 0;
for( Int i = 0; i < nEdges; ++i )
{
Int src = srcs[ind[i]];
while( curSrc <= src )
offsets[curSrc++] = i;
}
while(curSrc <= nVertices)
offsets[curSrc++] = nEdges;
}
//convert a list of edges into a CSC representation of the adjacency matrix
template<typename Int>
void edgeListToCSC(Int nVertices, Int nEdges
, const Int *srcs, const Int *dsts
, Int *offsets, Int *outSrcs, Int* sortIndices)
{
edgeListToCSR<Int>(nVertices, nEdges, dsts, srcs
, offsets, outSrcs, sortIndices);
};
//use gpu timer
struct GpuTimer
{
cudaEvent_t start;
cudaEvent_t stop;
GpuTimer()
{
cudaEventCreate(&start);
cudaEventCreate(&stop);
}
~GpuTimer()
{
cudaEventDestroy(start);
cudaEventDestroy(stop);
}
void Start()
{
cudaEventRecord(start, 0);
}
void Stop()
{
cudaEventRecord(stop, 0);
}
float ElapsedMillis()
{
float elapsed;
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsed, start, stop);
return elapsed;
}
};
#endif