# Can an ARRAY variable be both read and write?

**URL:** <https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870>\
**Category:** Support\
**Created:** [October 8, 2020, 11:45pm UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870 "2020-10-08T23:45:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sensei88](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@sensei88](https://discuss.pynq.io/u/sensei88)\
**Post date:** [October 8, 2020, 11:45pm UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/1 "2020-10-08T23:45:03Z")

</div>

I am implementing the following SAXPY function:  
Y = a\*X + Y  
where: X and Y are float vector(array) and a is a float scalar variable.

In HLS, I was able to confirm/simulate that array Y contains the updated value. But, when implemented in PYNQ, array Y is NOT updated and still contain the original value.

Any suggestions will be appreciated.

> \*\*\*PYNQ code as follows:  
> from pynq import Overlay  
> overlay = Overlay(‘/home/xilinx/pynq/overlays/saxpy/saxpy3.bit’)  
> sax=overlay.saxpy  
> from pynq import Xlnk  
> import numpy as np
> 
> # Allocate contiguious buffer for memory transfer
> 
> xlnk = Xlnk()  
> x\_buffer = xlnk.cma\_array(shape=(N,), dtype=np.float32)  
> y\_buffer = xlnk.cma\_array(shape=(N,), dtype=np.float32)
> 
> # Copy the DNA string to the in\_buffer
> 
> np.copyto(x\_buffer,vec\_x)  
> np.copyto(y\_buffer,vec\_y)
> 
> # initialize AXI with address and length,
> 
> sax.write(0x10,y\_buffer.physical\_address) # vector Y is AXI so need to initialize address  
> sax.write(0x18,x\_buffer.physical\_address) # vector Y is AXI so need to initialize address  
> sax.write(0x20,int(int(scalar\_a))) # initialize scalar A  
> sax.write(0x28,N) # initialize N (number of elements)  
> sax.write(0x00,0x01) # start  
> while sax.read(0x00 & 0x4)!= 0x04: # wait till its done  
> pass  
> sax.write(0x00,0x00) # stop  
> np.copy(vec\_y, y\_buffer)

\*\*\*HLS code as follows:

```auto
 #include <string.h>
 typedef float float_t ;
 void saxpy(volatile float_t *y, volatile const float_t *x, const float_t a, unsigned int N) {
 #pragma HLS INTERFACE m_axi port=y offset=slave depth=1000 bundle=OUT_Y
 #pragma HLS INTERFACE m_axi port=x offset=slave depth=1000 bundle=IN_X
 #pragma HLS INTERFACE s_axilite port=y bundle=ctrl
 #pragma HLS INTERFACE s_axilite port=x bundle=ctrl
 #pragma HLS INTERFACE s_axilite port=a bundle=ctrl
 #pragma HLS INTERFACE s_axilite port=N bundle=ctrl
 #pragma HLS INTERFACE s_axilite port=return bundle=ctrl
 	float x_buff[1000];
 	float y_buff[1000];
 	memcpy (y_buff, (float_t*) y, N*sizeof(float_t));
 	memcpy (x_buff, (const float_t*) x, N*sizeof(float_t));
 	unsigned int i;
 SAXLOOP: for (i=0;i<N;i++)
 #pragma HLS PIPELINE
 #pragma HLS LOOP_TRIPCOUNT min=5 max=1000
 		   y_buff[i] = a * x_buff[i] + y_buff[i];
 	memcpy ((float_t*) y, y_buff, N*sizeof(float_t));
 }
```

---

<div class="post-metadata">

**Author:** ![cathalmccabe](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/cathalmccabe/32/3348_2.png) [@cathalmccabe](https://discuss.pynq.io/u/cathalmccabe)\
**Post date:** [October 13, 2020, 10:39am UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/2 "2020-10-13T10:39:21Z")

</div>

Yes, you can have read/write on the same AXI master.

I edited your post to format the code properly. Some of the formatting was messed up by the markdown converter. I think the code is OK. You have additional volatiles and const I don’t think you need, which would mean you wouldn’t need to cast back to the float pointer, but I don’t think they matter.

You have left out some of the Python code. I’m not sure what values you initialised your arrays to, and what value int(int(scalar\_a)) has. ‘a’ would need to be a float so you may need to be careful with this when you write it.  
You might want to check the value you write by reading back sax.read(0x20)

If ‘a’ is zero, or very small, you may think Y is not updating as the algorithm is y[i] ~= y[i].

Cathal

---

<div class="post-metadata">

**Author:** ![sensei88](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@sensei88](https://discuss.pynq.io/u/sensei88)\
**Post date:** [October 13, 2020, 12:40pm UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/3 "2020-10-13T12:40:02Z")

</div>

Thanks Cathal for the feedback.

Actually scalar\_a should be floating-point. But I need to convert it to int because the register in axi-lite accepts only integer. Is there are suggestion how axi-lite accepts floating point?

I modified to code to handle integer datatype and it works 🙂 But floating-point still is a problem. I am intrigue with your last statement that “a” is zero or very small number… I will check by back reading sax.read(0x20). I wonder if there is a problem converting integer to floating point and vice-versa? For the python input, I assign scalar\_a with a value of 4.0

---

<div class="post-metadata">

**Author:** ![cathalmccabe](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/cathalmccabe/32/3348_2.png) [@cathalmccabe](https://discuss.pynq.io/u/cathalmccabe)\
**Post date:** [October 13, 2020, 1:10pm UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/4 "2020-10-13T13:10:37Z")

</div>

Yes, “scalar\_a” is float, but if you just convert to int, it will round it.  
E.g. 1.1 float is 0x3f8ccccd in hex, but int(1.1) is 1.  
You need to get the integer representation of the float.

I’m not sure if there is a better way, I’d use this:  
[https://stackoverflow.com/questions/23624212/how-to-convert-a-float-into-hex/38879403](https://stackoverflow.com/questions/23624212/how-to-convert-a-float-into-hex/38879403)

Cathal

---

<div class="post-metadata">

**Author:** ![bartokon](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@bartokon](https://discuss.pynq.io/u/bartokon)\
**Post date:** [October 14, 2020, 12:27pm UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/5 "2020-10-14T12:27:24Z")

</div>

Check this. I have created float simple adder

 ![image](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/9/96331d15835200ee3ae455fef3d3bbb1e8254ec0.png)

```
void addfloat(int a, int b, int& c) {
#pragma HLS INTERFACE ap_ctrl_none port=return
#pragma HLS INTERFACE s_axilite port=a
#pragma HLS INTERFACE s_axilite port=b
#pragma HLS INTERFACE s_axilite port=c

float afloat = *(float*)&a;
float bfloat = *(float*)&b;
float cfloat = afloat+bfloat;
c = *(int*)&cfloat;

```

}

---

<div class="post-metadata">

**Author:** ![sensei88](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@sensei88](https://discuss.pynq.io/u/sensei88)\
**Post date:** [October 14, 2020, 12:51pm UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/6 "2020-10-14T12:51:39Z")

</div>

Thank bartokon! That is a short and nice example of floating-point to integer 🙂 Will try to adopt your floating-to-integer conversion and vice versa and try it in the saxpy project.

---

<div class="post-metadata">

**Author:** ![sensei88](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@sensei88](https://discuss.pynq.io/u/sensei88)\
**Post date:** [October 14, 2020, 12:54pm UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/7 "2020-10-14T12:54:00Z")

</div>

I have questions … what does \*(float\*)&a as well as \*(int\*)&cfloat means?

Thanks…

---

<div class="post-metadata">

**Author:** ![cathalmccabe](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/cathalmccabe/32/3348_2.png) [@cathalmccabe](https://discuss.pynq.io/u/cathalmccabe)\
**Post date:** [October 14, 2020, 12:54pm UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/8 "2020-10-14T12:54:53Z")

</div>

@bartokon why are you using int’s on you HLS function when you want float’s? I think you should be able to replace all the casting by using floats, and replacing your code with:  
\*c = a+b; (where c is passed as a pointer)

I’m not sure that this casting/dereferencing is actually doing anything in HLS:

```auto
c = *(int*)&cfloat;

```

I think this should be the equivalent, although in your code you may want to cast the value of cfloat to an int:

```auto
c = cfloat; 

```

If it isn’t the same, I’d really like to understand why you did this.

Cathal

---

<div class="post-metadata">

**Author:** ![bartokon](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@bartokon](https://discuss.pynq.io/u/bartokon)\
**Post date:** [October 15, 2020, 7:43am UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/9 "2020-10-15T07:43:40Z")

</div>

Hmm, You mean something like that?:  
 ![cmp](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/b/b8cdf4d425e126ada86a55e2bc92ece9484f1cdc.png)

 ![cmp2](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/d/d10d83966f816ae5fa8eae79a7d1eb7ac5601541.png)  
 ![addno](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/0/0c1e87d1f858ae2f1589f7d6f85a6809d132305c.png) ![addstd](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/4/4cb579bc182a10f33c28c750e0ce752af2a85448.png)  
 ![addno1](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/2/2f5305075911761231d96f8ddd3d8eeccdada2bd.png) ![addstd1](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/0/08ecbd49cbfa9d778e8c86c65446df0f3e11666e.png)

If you would like me to check something just write full code and I will check 🙂

---

<div class="post-metadata">

**Author:** ![bartokon](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@bartokon](https://discuss.pynq.io/u/bartokon)\
**Post date:** [October 15, 2020, 7:55am UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/10 "2020-10-15T07:55:10Z")

</div>

> [@cathalmccabe](#):
>
> why are you using int’s on you HLS function when you want float’s? I think you should be able to replace all the casting by using floats, and replacing your code with:  
> \*c = a+b; (where c is passed as a pointer)

but you can’t write float to axi register

---

<div class="post-metadata">

**Author:** ![bartokon](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@bartokon](https://discuss.pynq.io/u/bartokon)\
**Post date:** [October 15, 2020, 7:55am UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/11 "2020-10-15T07:55:53Z")

</div>

If you could port this design to axi-stream and share code that would be great 😃

---

<div class="post-metadata">

**Author:** ![sensei88](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@sensei88](https://discuss.pynq.io/u/sensei88)\
**Post date:** [October 15, 2020, 8:51am UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/12 "2020-10-15T08:51:57Z")

</div>

Will complete my SAXPY floating-point project and share it with everybody.

Again @bartokon and @cathalmccabe for the helping out with questions.

Appreciate it!

---

<div class="post-metadata">

**Author:** ![bartokon](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@bartokon](https://discuss.pynq.io/u/bartokon)\
**Post date:** [October 15, 2020, 10:55am UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/13 "2020-10-15T10:55:24Z")

</div>

Wait a second! @cathalmccabe was right.  
 ![rep1](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/a/aa6a1fd95eb9ca440a67b5f0a1289e29b899c189.png)

 ![rep2](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/d/de14bd598a93858e518bbb3eb34f22fa45a3aad9.png)

---

<div class="post-metadata">

**Author:** ![sensei88](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@sensei88](https://discuss.pynq.io/u/sensei88)\
**Post date:** [October 15, 2020, 11:57am UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/14 "2020-10-15T11:57:42Z")

</div>

Curious why declare as float &c and not as pointer (float \*c)?

Though from your output it works.

---

<div class="post-metadata">

**Author:** ![bartokon](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@bartokon](https://discuss.pynq.io/u/bartokon)\
**Post date:** [October 15, 2020, 12:05pm UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/15 "2020-10-15T12:05:47Z")

</div>

Look here: [Overlay Tutorial — Python productivity for Zynq (Pynq)](https://pynq.readthedocs.io/en/v2.5.1/overlay_design_methodology/overlay_tutorial.html)

Also, I read somewhere that &should be used for writing (I think somewhere in Vitis documentation)

---

<div class="post-metadata">

**Author:** ![bartokon](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@bartokon](https://discuss.pynq.io/u/bartokon)\
**Post date:** [October 15, 2020, 12:23pm UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/16 "2020-10-15T12:23:38Z")

</div>

It also works on dma’s 😃  
 ![fix](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/7/7e1cb2269ea1f73c013e901b0c26905a0c6864b5.png)

 ![dma_float_1](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/6/6ea1a90a5e053fd9ef6d58dfa9096576b904c2db.png)  
 ![dma_float_2](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/5/553eef47b97b01b9dc3171944b434c73b5d3fbbe.png)

---

<div class="post-metadata">

**Author:** ![sensei88](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@sensei88](https://discuss.pynq.io/u/sensei88)\
**Post date:** [October 15, 2020, 12:25pm UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/17 "2020-10-15T12:25:46Z")

</div>

So focused with array (and pointers) that I forgot the basic 🙂

---

<div class="post-metadata">

**Author:** ![bartokon](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@bartokon](https://discuss.pynq.io/u/bartokon)\
**Post date:** [October 15, 2020, 12:42pm UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/18 "2020-10-15T12:42:24Z")

</div>

Apparently DMA doesn’t care if something is uint 32or float unlike the registers…

 ![lol](https://us1.discourse-cdn.com/flex019/uploads/pynq1/original/2X/2/2133a6178a96557a4dc8cdacaf786b473d67e990.png)

---

<div class="post-metadata">

**Author:** ![sensei88](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@sensei88](https://discuss.pynq.io/u/sensei88)\
**Post date:** [October 18, 2020, 12:52pm UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/19 "2020-10-18T12:52:03Z")

</div>

I finally solved the problem and completed the SAXPY IP project.

Thank you again @cathalmccabe for pointing out that _a_ could be a very small value (almost bordering 0) and that is what happen. The equation of saxpy is _y = a_x +y.

With _a_ almost zero, the equation becomes y[i] ~= y[i]. That is why I thought that array _y_ has the same value.

The culprit? _a = int(scalar\_a)_. For example: if scalar\_a = 4.0, then a = 4. But, when it is passed to the HLS (which is defined as float _a_), it is receiving a value of 0x00000004 which in float ( [IEEE-754](https://en.wikipedia.org/wiki/IEEE_754)) is an underflow (number so small it is approaching 0).

So, the solution is to translate the floating to its IEEE-754 representation. Again, @cathalmccabe has pointed the routine from stack overflow and modified by @bartokon to make it integer (instead of hex).

The function looks like this:

> import struct  
> def float\_to\_int(f):  
> return int(struct.unpack(‘\<I’, struct.pack(‘\<f’, f))[0])

So, a = float\_to\_int(scalar\_a)

The rest of the code listed above remains the same.

So, I guess that solves the mystery of the “array variable can be both read and write”.

As a takeaway, **simply converting float to integer using the int() function won’t work**!

---

<div class="post-metadata">

**Author:** ![bartokon](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@bartokon](https://discuss.pynq.io/u/bartokon)\
**Post date:** [October 18, 2020, 1:30pm UTC](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870/20 "2020-10-18T13:30:35Z")

</div>

@sensei88 one thing please double-check what happens if y \< 0. On float adder I had weird result’s coming back from accelerator!

[Next page](https://discuss.pynq.io/t/can-an-array-variable-be-both-read-and-write/1870.md?page=2)
