VexCL Reshape Behaving Strangely - opencl

I have the following code snippet testing the VexCL reshape function:
std::vector<int> ints;
for (int i = 0; i < n; i++) ints.push_back(i);
vex::vector<int> vex_ints(ctx, ints);
vex_ints = vex::reshape(vex_ints, vex::extents[2][n/2], vex::extents[1][0]);
for(int i=0; i<n; i++) std::cout << vex_ints[i] << " "; std::cout << std::endl;
All it does is print the even integers that are less than n followed by the odd integers that are less than n. For example when n=10, it prints:
0 2 4 6 8 1 3 5 7 9
But things behave strangely when n gets large. For example when n=10000, the first 50 printed integers are:
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 128 132 136 140 144 148 152 156 160 164 168 172 176 180 184 188 192 196
Which starts to get it wrong after 62. There are other discrepancies further down the output, and some even numbers occur after odd numbers. Can anybody explain why this is happening? If it matters, the context I used was "GeForce GT 650M (Apple)".

The kernel that is generated from your expression uses vex_ints both for input and output. Since what vex::reshape does is basically a permutation, you can not do this in place. Please try to assign the result to a different vector and see if this fixes the problem for you.

Related

Clock with loops in R to rename time-stamped files

I have to rename several tens of thousands of audio files of 5 seconds each, each of them coming from a file of 5 minutes (5minutes/5secondes = 60 files). To do this I need to define the time (hour, minute and second) of the beginning of the 5 minutes recording and I tried to make a clock that advances from 5 seconds to 5 seconds and that keep the values of seconds, minutes and hours in vectors to rename the files using these vectors like this:
stwd("")
name = "Car041512-2021-Pass1-Z2_20210914_211000_" #file name prefix
hours = 21
minutes = 19
seconde = 9
for (i in 0:59) {
seconde[i+1] = secondes + i*5
if(seconde[i+1] >= 60)
seconde[i+1] = seconde[i+1] - 60
minute[i+1]= minutes+1
if (minute >= 60)
minutes = 0
hour[i+1] = hours + 1
}
time = as.character(paste0(hour,minute,seconde))
list =list.files(all.files=F)
rename = paste0(name,time,".wav")
file.rename(list, rename)
I have a problem at the beginning of the loop. The seconds vector does not exceed 60 seconds but only during 2 cycles and I do not see why. This is the first time I've done loops with R and I must have made a lot of mistakes.
seconde
[1] 9 14 19 24 29 34 39 44 49 54 59 4 9 14 19 24 29 34 39 44 49 54 59 64 69 74 79 84 89 94 99 104 109 114 119 124 129 134 139 144 149 154 159 164 169 174 179 184 189 194 199 204 209 214 219 224 229 234 239 244
The renaming of the files works correctly, it's just the loop that doesn't work correctly. Can you help me?
Thanks in advance.

Planning Dates with R

i need to distribute some days along the year.
I have 213 activities and 247 days.. i need to plan this activities, but i need to cover the maximum time what can be possible.
I am substracting the total days - activities, in this case 34, i divide the total days with the previous result: 247/34= 7,26...
With this number i know what every seven days y have one without activity.
To code this part i doing this:
where day is a "for" variable what its looping a dataframe with dates and integer its the integer part of 7,26, in this case 7
if(day%%integer==0) {
aditional <- 0
} else {
aditional <- 1
}
#
if(day%%7==0) {
aditional <- 0
} else {
aditional <- 1
}
The result will be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
In bold font the day without activity
This way its cool, but its not so precise how i want.
I know i need to use the decimal part of the result of 7,26... 26, but i dont know how do it.
Can you help me please?
Thanks and sorry for my english
Make these 34 days the non-activity days:
round((247/34) * seq(34))
giving:
[1] 7 15 22 29 36 44 51 58 65 73 80 87 94 102 109 116 124 131 138
[20] 145 153 160 167 174 182 189 196 203 211 218 225 232 240 247

Looping and appending in R

I have the following R script
optioncost =c(5,52,23,15,134,996,2033,18)
options=c(0,1,1,1,1,0,1,1)
cip=c()
for (options_ind in options)
{
if(options_ind==1)
{
cip=append(cip,optioncost[which(options==options_ind)])
}
}
cip
I am trying to get (52 23 15 134 2033 18). Where as when I run the above script I get an output list which is 6 times the length of expected results. My output from the code is as follows for cip " 52 23 15 134 2033 18 52 23 15 134 2033 18 52 23 15 134 2033 18 52 23 15 134 2033 18 52 23 15 134 2033 18 52 23 15 134 2033 18".
Please help me find out where I have gone wrong?
optioncost[which(options==options_ind)] selects the information you want on its own.
The for loop is superfluous and in this case just repeats the process for as many "1"'s as there are in options, which is 6. Which is why your output data is 6 times larger than he data that you want.
optioncost[as.logical(options)]
If you want to work with a for loop then this the way to go
cip=c()
for (i in seq_along(options))
{
if(options[i]==1)
{
cip=append(cip,optioncost[i])
}
}
cip

AT+CSQ network percentage formula from rssi

Does anyone knows formula for calculating GSM network coverage into percents (0 .. 100) from rssi? It should be safe for 8bit AVR microcontroller CPU, without hardcore math operations like log or division by something that's not 2^n (bitshift is preferred). Creating array with 32 possible percentage values is poor solution.
possible rssi values (0..31 is valid values):
0 -113 dBm or less
1 -111 dBm
2...30 -109... -53 dBm
31 -51 dBm or greater
99 not known or not detectable
Approximate values i want:
RSSI %
0 0
1 3
2 6
3 10
4 13
5 16
6 19
7 23
8 26
9 29
10 32
11 36
12 39
13 42
14 45
15 48
16 52
17 55
18 58
19 61
20 65
21 68
22 71
23 74
24 78
25 81
26 84
27 87
28 90
29 94
30 97
31 100
99 ?
I'm out of ideas, so please advise me! Thanks for Your time!
(rssi * 827 + 127) >> 8
Multiply by 827, add 127 to simulate rounding to nearest, then drop the 8 low order bits, all in integer arithmetic.
This is unfortunately not a simple task to do if you want it to be accurate.
This article best explains the complexities of the task:
https://www.adriangranados.com/blog/dbm-to-percent-conversion
Without involving floats: RSSI*3+3 will miss high and low values, but will be OK mid-range. If accuracy at high values is more important add more than 3 and vice versa.

For loop in R with increments

I am trying to write a for loop which will increment its value by 2. The equivalent code is c is
for (i=0; i<=78; i=i+2)
How do I achieve the same in R?
See ?seq for more info:
for(i in seq(from=1, to=78, by=2)){
# stuff, such as
print(i)
}
or
for(i in seq(1, 78, 2))
p.s. Pardon my C ignorance. There, I just outed myself.
However, this is a way to do what you want in R (please see updated code)
EDIT
After learning a bit of how C works, it looks like the example posted in the question iterates over the following sequence: 0 2 4 6 8 ... 74 76 78.
To replicate that exactly in R, start at 0 instead of at 1, as above.
seq(from=0, to=78, by=2)
[1] 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44
[24] 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78
you can do so in following way, you can put any length upto which you want iteration in place of length(v1), and the increment value at position of 2 to your desired value
for(i in seq(1,length(v1),2))

Resources