Request for some comments on ats code

93 views
Skip to first unread message

chotu s

unread,
Feb 13, 2014, 11:20:33 PM2/13/14
to ats-lan...@googlegroups.com
For review purpose I have written a very tiny code for vectors in ats. If anyone can quickly go through it (whenever they get free time) and comment on the points below will be great. I have tried the code to be similar to examples given in "Effective ATS" .


Code is given here :  Vectors

1.  I want to know how to allocate the vector on a stack instead on a heap

2.  I want to know is vector_add function ok ?

3.  Is this how one would go about programming in ATS2

Any further comment is also welcome

Thanks

gmhwxi

unread,
Feb 14, 2014, 12:39:37 AM2/14/14
to ats-lan...@googlegroups.com

When you say 'allocate the vector on a stack', do you mean allocating
the arrayptr on stack?

gmhwxi

unread,
Feb 14, 2014, 12:52:16 AM2/14/14
to ats-lan...@googlegroups.com
In general, if the index n occurs in the type vector(a, n), then the size (of the type int(n)) should not be
stored inside a vector (of the type vector(a, n)). A big point in having dependent types is to allow a vector
and its size information to be stored separately.

gmhwxi

unread,
Feb 14, 2014, 12:57:03 AM2/14/14
to ats-lan...@googlegroups.com

chotu s

unread,
Feb 14, 2014, 2:13:02 AM2/14/14
to gmhwxi, ats-lan...@googlegroups.com
Thanks for reply.

I meant something as given here : 


That is I have abstract type vector and when I create a vector the amount of space need to store its content is taken from stack instead of heap. I want something like this :

val vect = make_local_vector_of_size_n (n)  

I assume that this will be again implementation dependent right ? 





--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/231c5688-dbf7-42d7-bb3e-27c04eb6868c%40googlegroups.com.

chotu s

unread,
Feb 14, 2014, 2:15:15 AM2/14/14
to gmhwxi, ats-lan...@googlegroups.com
So how do I determine the number of elements in arrayptr array. 

One way I think I could determine the total size in bytes of an array and then divide size of individual type , but how do I do this ? 
Or you are asking to pass "n" the size of vector to functions that need it.

Thanks



--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.

chotu s

unread,
Feb 14, 2014, 2:16:07 AM2/14/14
to gmhwxi, ats-lan...@googlegroups.com
Thanks I'll take a look soon.



--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.

gmhwxi

unread,
Feb 14, 2014, 8:23:03 AM2/14/14
to ats-lan...@googlegroups.com, gmhwxi

Allocating a vector of size n on stack for n that is unknown until run-time is not really
a good practice. There is really very little you can gain but you do run a big risk of stack overflow.

Brandon Barker

unread,
Feb 14, 2014, 8:23:49 AM2/14/14
to chotu s, gmhwxi, ats-lan...@googlegroups.com
For stack allocation my impression is that you allocate it as a tuple:
See this page and the next page:

I don't know how to write a function to allocate a tuple of arbitrary size, however.



Brandon Barker
brandon...@gmail.com


gmhwxi

unread,
Feb 14, 2014, 9:13:17 AM2/14/14
to ats-lan...@googlegroups.com, gmhwxi
Yes, you can alway pass it. Dependent typing ensures what is passed is always correct.
You can then build a convenience wrapper:

http://www.ats-lang.org/LIBRARY/contrib/libfloats/lavector.html

gmhwxi

unread,
Feb 14, 2014, 9:43:32 AM2/14/14
to ats-lan...@googlegroups.com, chotu s, gmhwxi
You can use alloca to allocate memory on stack at run-time.
Here is an example I did:

https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/alloca.dats

Brandon Barker

unread,
Feb 14, 2014, 10:09:12 AM2/14/14
to gmhwxi, ats-lan...@googlegroups.com, chotu s

gmhwxi

unread,
Feb 14, 2014, 10:20:07 AM2/14/14
to ats-lan...@googlegroups.com, gmhwxi, chotu s
Actually, most people do not realize that a real danger with alloca is that tail-recursion,
which should be done in constant space, may easily cause stack-overflow when it is coupled
with stack-allocation. The issue is very similar to inlining a function that calls alloca.

chotu s

unread,
Feb 14, 2014, 10:56:05 AM2/14/14
to gmhwxi, ats-lan...@googlegroups.com
Thanks a "lot" guys , I'll read all the suggestions and links tomorrow(as it is late here) and will ask related question after that.

Regarding stack allocation , what I had in mind was small size and temporary arrays whose size is known at compile time , some thing like val v = make_local_vector_n(n) , where n is known at compile time.


I think stack allocation idea came to me from the udacity parallel programming competition where allocating main memory on gpu was costly(if I remember correctly) , but they had different types of memory layer so one can also use the fast shared memory whose capacity ranged from 64K to 256K(subject to correction). 

Thanks for great suggestions.

gmhwxi

unread,
Feb 14, 2014, 11:18:13 AM2/14/14
to ats-lan...@googlegroups.com, gmhwxi
You are welcome.

A good design should decouple the issue of memory allocation
and object initialization:

extern
fun vector_make_ngc{n:int}{l:addr} (bytes(n) @ l | ptr (l)): vector

var bytes = @[100]() // statically allocated stack memory of 100 bytes
val vector = vector_make_ngc (view@bytes | addr@bytes)

Take a look at:

http://www.ats-lang.org/LIBRARY/libats/qlist.html

The functions qstruct_objectify and qstruct_unobjectify may shed some light on this.

chotu s

unread,
Feb 15, 2014, 5:23:51 AM2/15/14
to gmhwxi, ats-lan...@googlegroups.com
I can't seem to compile the code for :

#include "share/atspre_define.hats"
#include "share/atspre_staload.hats"
staload UN = "prelude/SATS/unsafe.sats"

implement main0 () = {

  var bytes = @[100]()
}


Or did you meant :

var bytes = @[byte][100]




gmhwxi

unread,
Feb 15, 2014, 9:57:06 AM2/15/14
to ats-lan...@googlegroups.com, gmhwxi
implement main0 () = {
  var bytes = @[char][100]()

chotu s

unread,
Feb 18, 2014, 8:43:48 AM2/18/14
to gmhwxi, ats-lan...@googlegroups.com
I can't seem to compile example  :

#include "share/atspre_define.hats"
#include "share/atspre_staload.hats"

extern
fun
vector_make_ngc{n:int}{l:addr} (bytes(n) @ l | ptr (l)): void


implement main0 () = {
  var bytes = @[byte][100]() // statically allocated stack memory of 100 bytes
  val () = vector_make_ngc (view@bytes | addr@bytes)
}
 





chotu s

unread,
Feb 18, 2014, 8:58:28 AM2/18/14
to Brandon Barker, ats-lan...@googlegroups.com
I don't think I need a implementation of vector_make_ngc  to compile the code , I only want it to type check , but correct me if I am wrong.


On Tue, Feb 18, 2014 at 7:24 PM, Brandon Barker <brandon...@gmail.com> wrote:
Where is the implementation for vector_make_ngc?

Brandon Barker
brandon...@gmail.com


Brandon Barker

unread,
Feb 18, 2014, 9:47:15 AM2/18/14
to ats-lan...@googlegroups.com, Brandon Barker
You are right, I was just curious to get a better idea of what was happening (if it existed).

I'm not really sure why what you are doing doesn't typecheck, but here is a version that does:

#include "share/atspre_define.hats"
#include "share/atspre_staload.hats"

staload "prelude/basics_sta.sats"
staload UN = "prelude/SATS/unsafe.sats"


extern
fun
vector_make_ngc{n:int}{l:addr} (!bytes(n) @ l | ptr (l)): void


implement main0 () = {
  var mybytes = @[byte][100]() // statically allocated stack memory of 100 bytes

  val (pf2, fpf2 | p2) = $UN.ptr0_vtake{bytes(100)}(addr@mybytes)
  val () = vector_make_ngc (pf2 | p2)
  prval() = fpf2(pf2)

}


Brandon Barker

unread,
Feb 18, 2014, 9:49:23 AM2/18/14
to ats-lan...@googlegroups.com, Brandon Barker
Also:
1) I don't think you need the first staload I had
2) If you replace the identifier 'mybytes' with 'bytes' you will run in to trouble.

chotu s

unread,
Feb 18, 2014, 10:12:47 AM2/18/14
to Brandon Barker, ats-lan...@googlegroups.com
Thanks.  It compiles now. I'll study the code.

 


gmhwxi

unread,
Feb 18, 2014, 2:32:05 PM2/18/14
to ats-lan...@googlegroups.com, Brandon Barker
Here is some code for doing stack allocation of a vector:

(* ****** ****** *)

abstype vector
(l:addr, n:int)

(* ****** ****** *)

extern
fun vector_make_ngc
 
{l:addr}{n:int} (b0ytes(n) @ l | ptr l): vector(l, n)
extern
fun vector_unmake_ngc
 
{l:addr}{n:int} (vector(l, n)): (bytes(n) @ l | ptr l)

(* ****** ****** *)

implement
main0
() =
{
var mybuf = @[byte][100]()
val myvec
= vector_make_ngc (view@mybuf | addr@mybuf)
val
(pf | p) = vector_unmake_ngc (myvec)
prval
() = view@mybuf := pf
}

(* ****** ****** *)

Brandon Barker

unread,
Feb 18, 2014, 2:56:17 PM2/18/14
to ats-lan...@googlegroups.com, Brandon Barker
I had forgotten the rather important point that the buffer had not been initialized. 

typedef bytes (n:int) = @[byte][n]

typedef b0ytes (n:int) = @[byte?][n]
Reply all
Reply to author
Forward
0 new messages