[erlang-questions] Is it possible to align binary's byte array to cache line boundary?

max tan maxtqm@REDACTED
Wed Oct 5 19:55:11 CEST 2011


Thank you very much Jon! And you Jesper!

Through some learning, I'm now sure that Haskell's Foreign Function Interface
( FFI ) provides the level of control I need. That is to say, I can
declare a instance
type (for example "MyType") of class "Storable", and overload the "sizeOf",
"alignment", "peek" and "poke" methods. Then I can provide "MyType" as type
parameter when using the facilities offerd by "Foreign.ForeignPtr",
"Foreign.Marshal",
"Foreign.Ptr", "Foreign.StablePtr", etc. Haskell's FFI will allocate
raw memory block
of "sizeOf" bytes starting from an address being multiples of
"alignment" on a heap
managed by Haskell RTS.

tqm

On Sun, Sep 25, 2011 at 1:35 AM, Jon Watte <jwatte@REDACTED> wrote:
> Maybe LazyByteStrings and Erlang binaries approach the performance of
> randomly aligned heap blocks in C.
>
> But, if I do data streaming processing (say, transforming vertices for 3D
> graphcs, transcoding media in multimedia, detecting intersections in rigid
> body dynamics, etc) then the specific alignment and stride of my data
> matters. Neither Erlang nor Haskell allows me to specify that "the first
> byte of this byte string is always aligned on a multiple of 16 bytes" (for
> architecture-specific SIMD exploitation) or "the first byte of this byte
> string is always aligned on a multiple of 64 bytes" (for
> architecture-specific cache line exploitation) or "no word within this
> binary will share a L3 cache line with any other word allocated in the same
> manner" (for architecture-specific cache contention reduction for
> spinlocks).
>
> C allows you to control these things. That's why it's a systems programming
> language, and Erlang isn't (although it's a language for programming systems
> :-)
>
> Sincerely,
>
> jw
>
> --
> Americans might object: there is no way we would sacrifice our living
> standards for the benefit of people in the rest of the world. Nevertheless,
> whether we get there willingly or not, we shall soon have lower consumption
> rates, because our present rates are unsustainable.
>
>
>
> On Thu, Sep 22, 2011 at 1:46 PM, Jesper Louis Andersen
> <jesper.louis.andersen@REDACTED> wrote:
>>
>> On Thu, Sep 22, 2011 at 17:49, max tan <maxtqm@REDACTED> wrote:
>>
>> >
>> > I just found Haskell has "Data.ByteString.Lazy", according to a paper
>> > titled
>> > "Rewriting Haskell Strings":
>> >
>> >   A library for ByteStrings is implemented,   providing a purely
>> > functional
>> > interface, which approaches the speed of low-level mutable arrays in C.
>> > _______________________________________________
>>
>> This is true. In many ways, a Lazy ByteString in Haskell corresponds
>> to a list of binaries in Erlang, which is a subset of the iolist()
>> type in Erlang. If Haskells Lazy Bytestring implementation approaches
>> the speed of low-level mutable arrays, so does binaries in Erlang.
>>
>> My experience is they perform at about the same speed. I don't know
>> what happened in my brain when I called Haskell a dynamic language
>> though. It is statically typed, and very much so.
>>
>>
>> --
>> J.
>
>



More information about the erlang-questions mailing list