Using pointers for image objects in opencl - opencl

I am trying to read the image objects using a pointer instead of 2D image object buffer
img_fmt.image_channel_order = CL_R;
img_fmt.image_channel_data_type = CL_UNORM_INT8;
__kernel void test(__read_only image2d_t in, __write_only image2d_t out)
{
float *p;
void *temp;
temp = (void *)∈
p=temp;
if(get_global_id(0) == 10)
printf("%f",*p);
}
However when I get the print of this I see all the values as zero's.
I want to do this
1) find out the reason why its happening?
2) if the above works I want to use vload16 on pointer p to get 16 pixels in one go? I don't think its possible to get 16 pixels from the address of the image object, but I want to see what is there in memory at that address?
One more interesting thing I saw when I just print the address of in on each work item id. Using below code:
__kernel void test(__read_only image2d_t in, __write_only image2d_t out)
{
float *p;
void *temp;
temp = (void *)∈
if(get_global_id(0) < 50)
printf("%p",temp);
}
0x500000000 row no 0 col no 0 0x500004000 row no 0 col no 4 0x500008000 row no 0 col no 8 0x50000C000 row no 0 col no 12 0x500000080 row no 0 col no 16 0x500004080 row no 0 col no 20
0x500000008 row no 0 col no 1 0x500004008 row no 0 col no 5 0x500008008 row no 0 col no 9 0x50000C008 row no 0 col no 13 0x500000088 row no 0 col no 17 0x500004088 row no 0 col no 21
0x500000010 row no 0 col no 2 0x500004010 row no 0 col no 6 0x500008010 row no 0 col no 10 0x50000C010 row no 0 col no 14 0x500000090 row no 0 col no 18 0x500004090 row no 0 col no 22
0x500000018 row no 0 col no 3 0x500004018 row no 0 col no 7 0x500008018 row no 0 col no 11 0x50000C018 row no 0 col no 15 0x500000098 row no 0 col no 19 0x500004098 row no 0 col no 23
0x500000020 row no 1 col no 0 0x500004020 row no 1 col no 4 0x500008020 row no 1 col no 8 0x50000C020 row no 1 col no 12 0x5000000A0 row no 1 col no 16 0x5000040A0 row no 1 col no 20
0x500000028 row no 1 col no 1 0x500004028 row no 1 col no 5 0x500008028 row no 1 col no 9 0x50000C028 row no 1 col no 13 0x5000000A8 row no 1 col no 17 0x5000040A8 row no 1 col no 21
0x500000030 row no 1 col no 2 0x500004030 row no 1 col no 6 0x500008030 row no 1 col no 10 0x50000C030 row no 1 col no 14 0x5000000B0 row no 1 col no 18 0x5000040B0 row no 1 col no 22
0x500000038 row no 1 col no 3 0x500004038 row no 1 col no 7 0x500008038 row no 1 col no 11 0x50000C038 row no 1 col no 15 0x5000000B8 row no 1 col no 19 0x5000040B8 row no 1 col no 23
0x500000040 row no 2 col no 0 0x500004040 row no 2 col no 4 0x500008040 row no 2 col no 8 0x50000C040 row no 2 col no 12 0x5000000C0 row no 2 col no 16 0x5000040C0 row no 2 col no 20
0x500000048 row no 2 col no 1 0x500004048 row no 2 col no 5 0x500008048 row no 2 col no 9 0x50000C048 row no 2 col no 13 0x5000000C8 row no 2 col no 17 0x5000040C8 row no 2 col no 21
0x500000050 row no 2 col no 2 0x500004050 row no 2 col no 6 0x500008050 row no 2 col no 10 0x50000C050 row no 2 col no 14 0x5000000D0 row no 2 col no 18 0x5000040D0 row no 2 col no 22
0x500000058 row no 2 col no 3 0x500004058 row no 2 col no 7 0x500008058 row no 2 col no 11 0x50000C058 row no 2 col no 15 0x5000000D8 row no 2 col no 19 0x5000040D8 row no 2 col no 23
0x500000060 row no 3 col no 0 0x500004060 row no 3 col no 4 0x500008060 row no 3 col no 8 0x50000C060 row no 3 col no 12 0x5000000E0 row no 3 col no 16 0x5000040E0 row no 3 col no 20
0x500000068 row no 3 col no 1 0x500004068 row no 3 col no 5 0x500008068 row no 3 col no 9 0x50000C068 row no 3 col no 13 0x5000000E8 row no 3 col no 17 0x5000040E8 row no 3 col no 21
0x500000070 row no 3 col no 2 0x500004070 row no 3 col no 6 0x500008070 row no 3 col no 10 0x50000C070 row no 3 col no 14 0x5000000F0 row no 3 col no 18 0x5000040F0 row no 3 col no 22
0x500000078 row no 3 col no 3 0x500004078 row no 3 col no 7 0x500008078 row no 3 col no 11 0x50000C078 row no 3 col no 15 0x5000000F8 row no 3 col no 19 0x5000040F8 row no 3 col no 23
Each 4x4 tile is stored in continuous address space. Actually there is pattern to store the tiles Like 0th and 4th 4x4 block are also contiguous in memory.
I am not able to connect all things. If anyone can help that will be wonderful.

You simply cannot do what you are trying; the image2d_t type is not a pointer. If your particular implementation happens to let you get at something that looks like image data using it that way, it is just an artifact of the implementation and you can't expect it to be portable or even work with the next driver version.
Perhaps what you want is device memory you can access either as a buffer or as an image. If so, use the cl_khr_image2d_from_buffer extension.

Related

R: Calculation combinations and variable iteration for loop

I want to calculate combinations in R.
I want to calculate and get results as in the below code, but in my code, the number in the for loop depends on the number of variables (e.g., length(ncomb)).
How do I set the number in a for loop?
Or is there a better way to calculate the combinations that I want?
#Block
nblock = c(1,2,3)
num_nblock = length(nblock)
#Position
tol = c(1:6)
total = length(tol)
#Calculate number of Combination
#6C1*5C2*3C3
t1 = total
ncomb=c()
for (i in 1:num_nblock) {
ncomb[i] = choose(t1,nblock[i])
t1 = t1-nblock[i]
}
#Calculate Combination
Clist = data.frame()
for (i in 1:ncomb[1]) {
comb1 = combn(total,nblock[1])
remain = setdiff(tol,comb1[,i])
for (j in 1:ncomb[2]) {
comb2 = combn(remain,nblock[2])
remain2 = setdiff(remain,comb2[,j])
for (k in 1:ncomb[3]) {
comb3 = combn(remain2,nblock[3])
ans = c(comb1[,i],comb2[,j],comb3[,k])
Clist =rbind(Clist,ans)
}
}
}
#Result :Clist
X1L X2L X3L X4L X5L X6L
1 1 2 3 4 5 6
2 1 2 4 3 5 6
3 1 2 5 3 4 6
4 1 2 6 3 4 5
5 1 3 4 2 5 6
6 1 3 5 2 4 6
7 1 3 6 2 4 5
8 1 4 5 2 3 6
9 1 4 6 2 3 5
10 1 5 6 2 3 4
.....
50 5 4 6 1 2 3
51 6 1 2 3 4 5
52 6 1 3 2 4 5
53 6 1 4 2 3 5
54 6 1 5 2 3 4
55 6 2 3 1 4 5
56 6 2 4 1 3 5
57 6 2 5 1 3 4
58 6 3 4 1 2 5
59 6 3 5 1 2 4
60 6 4 5 1 2 3
So here is an idea I have which may be harder to understand but solves your problem of having a variable number of for loops.
Before I show my code, let me explain the idea using your example of dividing 1 through 6 into blocks of 1, 2, and 3. As you said we can calculate the total number of combinations as 6C1*5C2*3C3=60. Now the question is how to fill up the 60 entries.
So if you think about a tree from Block 1 to 3, each branch of Block 1 correspond to 5C2 number of branches of Block2, and each branch of Block 2 correspond to 3C3 branch of Block 3. In this way, the total number of branches will be 6C1*5C2*3C3=60. Essentially how you wanna fill up the output matrix is to repeat each branch in Block 1 5C2*3C3 times, each branch in Block 2 3C3 time, and each branch in Block 3 should appear uniquely. To summarize you want to repeat each branch the number of times to the "cardinality" of Blocks to the right hand side.
This is what the following code is doing.
# ++++ Using your example and initialization ++++
# Block
nblock = c(1,2,3)
num_nblock = length(nblock)
# Position
tol = c(1:6)
total = length(tol)
t1 = total
ncomb=c()
for (i in 1:num_nblock) {
ncomb[i] = choose(t1,nblock[i])
t1 = t1-nblock[i]
}
# ++++++++
# Initialize result matrix
Clist = matrix(nrow = prod(ncomb), ncol = total)
# Block col ID: produce list of (1),(2,3),(4,5,6) as col ID of output matrix
block_cols = list()
start = 1
for (i in 1:num_nblock) {
block_cols[[i]] = start:(start+nblock[i]-1)
start = start + nblock[i]
}
# Fill the output matrix: iterate each (row,block) of matrix
for (i in 1:prod(ncomb)) {
for (j in 1:num_nblock) {
# First col ID of each block. In this example, always 1, 2, 4
block_first_col_id = block_cols[[j]][1]
# Fill the pos when its still NA
if (is.na(Clist[i, block_first_col_id])) {
# Filler is all combination having removed numbers appeared in left blocks
remain = setdiff(tol, Clist[i, 0:(block_first_col_id-1)])
com = combn(remain, nblock[j])
# Key step: replicate to fill remaining cardinality
filler = apply(com, 1, function(x) rep(x, each = prod(ncomb[(j+1):length(ncomb)])))
# Store filler to output.
# Filler may be a vector, in which case dim will return NULL
filler_nrow = ifelse(is.null(dim(filler)[1]), 1, dim(filler)[1])
Clist[i:(i + filler_nrow - 1), block_cols[[j]]] = filler
}
}
}

Change rows index dataframe R

I'm new on programming on R and I need a simple thing. I have a dataframe like this:
A number
3 1 3
4 1 4
11 2 11
12 2 12
18 3 18
19 3 19
the first column is the one obtained by R default. I'd like to exchange this one with the column "number" always having the name of the column. Something like this:
number A
3 1
4 1
11 2
12 2
18 3
19 3
I need to do it because it is a large dataset and going on the correspondence between two columns is lost.
It seems like you want to remove your row names?
df <- data.frame("Colours" = c("Red", "Red", "Green", "Yellow"),
"Number" = c(1,2,3,6))
rownames(df) <- c(1,2,3,6)
df
Colours Number
1 Red 1
2 Red 2
3 Green 3
6 Yellow 6
Setting rownames as NULL, we will remove the row names and they will be called by just row number now.
rownames(df) <- NULL
df
Colours Number
1 Red 1
2 Red 2
3 Green 3
4 Yellow 6

Select from column in dataframe based on value in another column

I have a dataframe as follows:
dataDF <- data.frame(
id = 1:5,
to_choose = c('red', 'blue', 'red', 'green', 'yellow'),
red_value = c(1,2,3,4,5),
blue_value = c(6,7,8,9,10),
yellow_value = c(11,12,13,14,15)
)
id to_choose red_value blue_value yellow_value
1 red 1 6 11
2 blue 2 7 12
3 red 3 8 13
4 green 4 9 14
5 yellow 5 10 15
I want to create a new column value, which is the value from the appropriate column based on the to_choose column.
I could do this with an ifelse as follows
mutate(dataDF,
value = ifelse(to_choose == 'red', red_value,
ifelse(to_choose == 'blue', blue_value,
ifelse(to_choose == 'yellow', yellow_value, NA))))
To give
id to_choose red_value blue_value yellow_value value
1 red 1 6 11 1
2 blue 2 7 12 7
3 red 3 8 13 3
4 green 4 9 14 NA
5 yellow 5 10 15 15
But if there a simpler one line way of doing this along the lines of
mutate(dataDF, value = paste(to_choose, 'value', sep = '_'))
dataDF %>%
gather(var, value , 3:5) %>%
mutate(var = gsub('_value', '', var)) %>%
filter(to_choose == var)
A base R approach using mapply
dataDF$value <- mapply(function(x, y) if(length(y) > 0) dataDF[x, y] else NA,
1:nrow(dataDF), sapply(dataDF$to_choose, function(x) grep(x, names(dataDF))))
dataDF
# id to_choose red_value blue_value yellow_value value
#1 1 red 1 6 11 1
#2 2 blue 2 7 12 7
#3 3 red 3 8 13 3
#4 4 green 4 9 14 NA
#5 5 yellow 5 10 15 15
The idea is to get the appropriate row and column indices to subset upon. Row indices we are already know that we need to get value for each row of the dataframe. As far as getting the appropriate column is concerned we use grep over to_choose to find the column index from where the value needs to be extracted.

How to sort the columns of every row of a data frame and then save the column name in the corresponding columns in R?

I have a data frame A.
A <- as.data.frame(matrix(c(2,3,1,8,6,7,5,9,4), nrow = 3, ncol = 3))
rownames(A)<-c('Row1','Row2','Row3')
colnames(A)<-c('Col1','Col2','Col3')
A
Col 1 Col 2 Col 3
Row 1 2 8 5
Row 2 3 6 9
Row 3 1 7 4
I want to sort every column of a row in descending order and copy the column name in the respective columns like this.
Row 1 Col 2 Col 3 Col 1
Row 2 Col 3 Col 2 Col 1
Row 3 Col 2 Col 3 Col 1
I used this code to achieve this.
sorted_users_vs_tags1<-t(apply(sorted_users_vs_tags1, 1, function(row) {
names(row)<-colnames(sorted_users_vs_tags1)
row[, order(row, decreasing = TRUE)]
row<-names(row)})
But I am getting an error in dimension length in 2nd line of the function in apply.
Use this
matrix(colnames(A)[t(apply(A,1,order,decreasing=T))],ncol=3)

Obtaining data from a different row of data frame

I have a large data frame. Each row has data for a specific date. The next group of columns has stock prices. Each column represents one stock. I then have offset columns, one for each stock column. I have to offset the current row by the offset amount. Then I put the prices found in the first group of columns (but now using the offset row) in the last group of columns, which start out as NA.
For example, the value in row 1, col 3 is 1, so I need to offset the first row by 1. That gives me row 2. I need to get the price, p1, that is in row 2, col 1. That value is 2. The value 2 is then placed into row 1, col 5.
I theoretically solved the problem with a double loop, but the code was hopelessly slow. I was able to eliminate one loop. Can someone please help me eliminate the remaining loop?
Below is my code as well as the data frame before and after the code runs. Note that in the sample, I omitted the dates as they are not needed.
p1 = 1:1000000
p2 = 11:1000010
of1 = c(rep(1, 100000), rep(2, 800000), rep(0, 100000) )
of2 = c(rep(2,100000),rep(1,800000), rep(0, 100000) )
DF1 = data.frame(p1 = p1, p2 = p2, of1 = of1, of2 = of2)
DF1$newPrice1 = rep(NA, 1000000)
DF1$newPrice2 = rep(NA, 1000000)
head(DF1)
p1 p2 of1 of2 newPrice1 newPrice2
1 1 11 1 2 NA NA
2 2 12 1 2 NA NA
3 3 13 1 2 NA NA
4 4 14 1 2 NA NA
5 5 15 1 2 NA NA
6 6 16 1 2 NA NA
for(j in 1:2) {
DF1[j+4] = DF1[DF1[,j+2] + row(DF1)[,j], j]
}
head(DF1)
p1 p2 of1 of2 newPrice1 newPrice2
1 1 11 1 2 2 13
2 2 12 1 2 3 14
3 3 13 1 2 4 15
4 4 14 1 2 5 16
5 5 15 1 2 6 17
6 6 16 1 2 7 18
DF1$np1 <- DF1$p1[seq_along(DF1$p1) + DF1$of1]
DF1$np2 <- DF1$p2[seq_along(DF1$p2) + DF1$of2]
identical(DF1$np1, DF1$newPrice1)
#[1] TRUE
identical(DF1$np2, DF1$newPrice2)
#[1] TRUE

Resources