File: utilities.cpp

package info (click to toggle)
rocr-runtime 6.4.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,928 kB
  • sloc: cpp: 126,824; ansic: 41,837; lisp: 1,225; asm: 905; sh: 452; python: 117; makefile: 59
file content (193 lines) | stat: -rw-r--r-- 4,597 bytes parent folder | download
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "utilities.h"

/*
 * Prints no more than 256 elements of the given array.
 * Prints full array if length is less than 256.
 * Prints Array name followed by elements.
 */
template<typename T> 
void PrintArray(
    string header, 
    const T * data, 
    const int width,
    const int height) 
{
    cout<<"\n"<<header<<"\n";
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            cout<<data[i*width+j]<<" ";
        }
        cout<<"\n";
    }
    cout<<"\n";
}

template<typename T>
int IsPowerOf2(T val)
{
    long long _val = val;
    if((_val & (-_val))-_val == 0 && _val != 0)
        return 0;
    else
        return -1;
}


template<typename T>
T RoundToPowerOf2(T val)
{
    int bytes = sizeof(T);

    val--;
    for(int i = 0; i < bytes; i++)
        val |= val >> (1<<i);  
    val++;

    return val;
}

template<typename T> 
int FillRandom(
         T * arrayPtr, 
         const int width,
         const int height,
         const T rangeMin,
         const T rangeMax,
         unsigned int seed)
{
    if(!arrayPtr)
    {
        printf("Cannot fill array. NULL pointer.");
        return -1;
    }

    if(!seed)
        seed = (unsigned int)time(NULL);

    srand(seed);
    double range = double(rangeMax - rangeMin) + 1.0; 

    /* random initialisation of input */
    for(int i = 0; i < height; i++)
        for(int j = 0; j < width; j++)
        {
            int index = i*width + j;
            arrayPtr[index] = rangeMin + T(range*rand()/(RAND_MAX + 1.0)); 
        }

    return 0;
}

#if 0
//get a memory region that can be used for global memory allocations.
hsa_status_t get_global_region(hsa_region_t region, void* data) 
{
	hsa_region_segment_t segment;
	hsa_region_get_info(region, HSA_REGION_INFO_SEGMENT, &segment);
	if (HSA_REGION_SEGMENT_GLOBAL == segment) 
	{
		hsa_region_t* ret = (hsa_region_t*) data;
		*ret = region;
	}
	return HSA_STATUS_SUCCESS;
}


/*
 * Finds the specified symbols offset in the specified brig_module.
 * If the symbol is found the function returns HSA_STATUS_SUCCESS, 
 * otherwise it returns HSA_STATUS_ERROR.
 */
hsa_status_t find_symbol_offset(hsa_ext_brig_module_t* brig_module, 
		char* symbol_name,
		hsa_ext_brig_code_section_offset32_t* offset) 
{

	/*  
	 * Get the data section 
	 */
	hsa_ext_brig_section_header_t* data_section_header = 
		brig_module->section[HSA_EXT_BRIG_SECTION_DATA];
	/*  
	 * Get the code section
	 */
	hsa_ext_brig_section_header_t* code_section_header =
		brig_module->section[HSA_EXT_BRIG_SECTION_CODE];

	/*  
	 * First entry into the BRIG code section
	 */
	BrigCodeOffset32_t code_offset = code_section_header->header_byte_count;
	BrigBase* code_entry = (BrigBase*) ((char*)code_section_header + code_offset);
	while (code_offset != code_section_header->byte_count) 
	{
		if (code_entry->kind == BRIG_KIND_DIRECTIVE_KERNEL) 
		{
			/* 
			 * Now find the data in the data section
			 */
			BrigDirectiveExecutable* directive_kernel = (BrigDirectiveExecutable*) (code_entry);
			BrigDataOffsetString32_t data_name_offset = directive_kernel->name;
			BrigData* data_entry = (BrigData*)((char*) data_section_header + data_name_offset);
			if (!strncmp(symbol_name, (char*) data_entry->bytes, strlen(symbol_name))) 
			{
				*offset = code_offset;
				return HSA_STATUS_SUCCESS;
			}
		}
		code_offset += code_entry->byteCount;
		code_entry = (BrigBase*) ((char*)code_section_header + code_offset);
	}   
	return HSA_STATUS_ERROR;
}
#endif

/*
 * Determines if the given agent is of type HSA_DEVICE_TYPE_GPU
 * and sets the value of data to the agent handle if it is.
 */
hsa_status_t find_gpu(hsa_agent_t agent, void *data) 
{
	if (data == NULL) 
	{
		return HSA_STATUS_ERROR_INVALID_ARGUMENT;
	}   
	hsa_device_type_t device_type;
	hsa_status_t stat = hsa_agent_get_info(agent, HSA_AGENT_INFO_DEVICE, &device_type);
	if (stat != HSA_STATUS_SUCCESS) 
	{
		return stat;
	}   
	if (device_type == HSA_DEVICE_TYPE_GPU) 
	{
		*((hsa_agent_t *)data) = agent;
	}   
	return HSA_STATUS_SUCCESS;
}


/*
 * Determines if a memory region can be used for kernarg
 * allocations.
 */
hsa_status_t get_memory_region(hsa_region_t region, void* data) 
{
	hsa_region_global_flag_t flags;
	hsa_region_get_info(region, HSA_REGION_INFO_GLOBAL_FLAGS, &flags);

	MemRegion *my_mem_region = (MemRegion *)data;
	
	if (flags & HSA_REGION_GLOBAL_FLAG_COARSE_GRAINED) {
             my_mem_region->coarse_region = region;
       }
	
	if (flags & HSA_REGION_GLOBAL_FLAG_KERNARG) 
	{
		my_mem_region->kernarg_region= region;
	}   
	
	return HSA_STATUS_SUCCESS;
}