Re: Vector operations optimization.

2012-03-22 Thread James Miller
On 23 March 2012 18:57, Comrad  wrote:
> On Thursday, 22 March 2012 at 10:43:35 UTC, Trass3r wrote:
>>>
>>> What is the status at the moment? What compiler and with which compiler
>>> flags I should use to achieve maximum performance?
>>
>>
>> In general gdc or ldc. Not sure how good vectorization is though, esp.
>> auto-vectorization.
>> On the other hand the so called vector operations like a[] = b[] + c[];
>> are lowered to hand-written SSE assembly even in dmd.
>
>
> I had such a snippet to test:
>
>  1 import std.stdio;
>  2 void main()
>  3 {
>  4   double[2] a=[1.,0.];
>  5   double[2] a1=[1.,0.];
>  6   double[2] a2=[1.,0.];
>  7   double[2] a3=[0.,0.];
>  8   foreach(i;0..10)
>  9     a3[]+=a[]+a1[]*a2[];
>  10   writeln(a3);
>  11 }
>
> And I compared with the following d code:
>
>  1 import std.stdio;
>  2 void main()
>  3 {
>  4   double[2] a=[1.,0.];
>  5   double[2] a1=[1.,0.];
>  6   double[2] a2=[1.,0.];
>  7   double[2] a3=[0.,0.];
>  8   foreach(i;0..10)
>  9   {
>  10     a3[0]+=a[0]+a1[0]*a2[0];
>  11     a3[1]+=a[1]+a1[1]*a2[1];
>  12   }
>  13   writeln(a3);
>  14 }
>
> And with the following c code:
>
>  1 #include  
>  2 int main()
>  3 {
>  4   double a[2]={1.,0.};
>  5   double a1[2]={1.,0.};
>  6   double a2[2]={1.,0.};
>  7   double a3[2];
>  8   unsigned i;
>  9   for(i=0;i<10;++i)
>  10   {
>  11     a3[0]+=a[0]+a1[0]*a2[0];
>  12     a3[1]+=a[1]+a1[1]*a2[1];
>  13   }
>  14   printf("%f %f\n",a3[0],a3[1]);
>  15   return 0;
>  16 }
>
> The last one I compiled with gcc two previous with dmd and ldc. C code with
> -O2
> was the fastest and as fast as d without slicing compiled with ldc. d code
> with slicing was 3 times slower (ldc compiler). I tried to compile with
> different optimization flags, that didn't help. Maybe I used the wrong ones.
> Can someone comment on this?

The flags you want are -O2, -inline -release.

If you don't have those, then that might explain some of the slow down
on slicing, since -release drops a ton of runtime checks.

Otherwise, I'm not sure why its so much slower, the druntime array ops
are written using SIMD instructions where available, so it should be
fast.

--
James Miller


Re: Freeing memory allocated at C function

2012-03-22 Thread Ali Çehreli

On 03/22/2012 11:27 PM, Pedro Lacerda wrote:
> I'm using some C functions like these:
>
>  char *str = allocateNewString();
>
> And this:
>
>  Object *obj = constructObject();
>  // etc
>  freeObject(obj);
>
>
> Do I need to free the memory in both cases? Can I someway register 
them on

> GC?
>

You can register on GC if you wrap the resources in a class. Then the 
class object's destructor would call the clean up code. The problem is, 
it is undeterministic when the destructor will be called, or will it be 
called at all!


Or you can wrap in a struct which has deterministic destruction like in 
C++, when leaving scopes.


A better thing to do in this case is to use the scope() statement. 
Depending of when you want the cleanup to happen:


- scope(success): if the scope is being exited successfully

- scope(failure): if the scope is being exited with an exception

- scope(exit): when exiting the scope regardless

For example, if you want the cleanup only if there is an exception:

int allocate()
{
return 42;
}

void deallocate(int)
{}

void foo()
{
int resource = allocate();
scope(failure) deallocate(resource);

// ... an exception may be thrown here ...
}

void main()
{
foo();
}

Ali



Freeing memory allocated at C function

2012-03-22 Thread Pedro Lacerda
I'm using some C functions like these:

char *str = allocateNewString();

And this:

Object *obj = constructObject();
// etc
freeObject(obj);


Do I need to free the memory in both cases? Can I someway register them on
GC?


Re: Vector operations optimization.

2012-03-22 Thread Comrad

On Thursday, 22 March 2012 at 10:43:35 UTC, Trass3r wrote:
What is the status at the moment? What compiler and with which 
compiler flags I should use to achieve maximum performance?


In general gdc or ldc. Not sure how good vectorization is 
though, esp. auto-vectorization.
On the other hand the so called vector operations like a[] = 
b[] + c[]; are lowered to hand-written SSE assembly even in dmd.


I had such a snippet to test:

  1 import std.stdio;
  2 void main()
  3 {
  4   double[2] a=[1.,0.];
  5   double[2] a1=[1.,0.];
  6   double[2] a2=[1.,0.];
  7   double[2] a3=[0.,0.];
  8   foreach(i;0..10)
  9 a3[]+=a[]+a1[]*a2[];
 10   writeln(a3);
 11 }

And I compared with the following d code:

  1 import std.stdio;
  2 void main()
  3 {
  4   double[2] a=[1.,0.];
  5   double[2] a1=[1.,0.];
  6   double[2] a2=[1.,0.];
  7   double[2] a3=[0.,0.];
  8   foreach(i;0..10)
  9   {
 10 a3[0]+=a[0]+a1[0]*a2[0];
 11 a3[1]+=a[1]+a1[1]*a2[1];
 12   }
 13   writeln(a3);
 14 }

And with the following c code:

  1 #include  
  2 int main()
  3 {
  4   double a[2]={1.,0.};
  5   double a1[2]={1.,0.};
  6   double a2[2]={1.,0.};
  7   double a3[2];
  8   unsigned i;
  9   for(i=0;i<10;++i)
 10   {
 11 a3[0]+=a[0]+a1[0]*a2[0];
 12 a3[1]+=a[1]+a1[1]*a2[1];
 13   }
 14   printf("%f %f\n",a3[0],a3[1]);
 15   return 0;
 16 }

The last one I compiled with gcc two previous with dmd and ldc. C 
code with -O2
was the fastest and as fast as d without slicing compiled with 
ldc. d code with slicing was 3 times slower (ldc compiler). I 
tried to compile with different optimization flags, that didn't 
help. Maybe I used the wrong ones. Can someone comment on this?


Re: problems countered after while(read()){} terminated with ^D or EOF

2012-03-22 Thread Andrej Mitrovic
On 3/22/12, Tyro[17]  wrote:
> issue #2
>  how do i read a string[] such that whitespace (all or one of
> my choosing) delineate the string boundary?

Jesse Phillips has a cmdln.interact library that I think would work by using:
string[] result = userInput!(string[])("Enter space-delimited values:");
But it seems he changed his nickname on github and hasn't reuploaded
the library yet.

In case you're ever reading from a file you can do:
string input = cast(string)std.file.read("filename");
string[] text = input.split();


problems countered after while(read()){} terminated with ^D or EOF

2012-03-22 Thread Tyro[17]
I'm using the following to read arrays from the command line (or 
redirected file) but am having some issues that I have not been 
able to solve on my own. Would appreciate if a little guidance.


void f(T)(ref T a)if(isArray!T)
{
a.length = 100;
int i;
while(readf(" %s", &a[i++])) // #1
if(i == a.length) a.length *= 2;
a.length[--i];
}

issue #1
how do it terminate the input (or clear the buffer) such that 
subsequent calls to readf are not ignored?


int[] i;
f(i); // no matter how many time I call readf() after this 
point it is all always ignored

double d;
readf(" %s", &d); // not called

issue #2
how do i read a string[] such that whitespace (all or one of 
my choosing) delineate the string boundary?


readf(" %s ", &data)  sort of does the trick for spaces but 
it leaves all newlines embedded in substrings.  Assuming that 
input is provided one string per line, readf(" %s\n", &data) 
works similar to #1 above, however if there is multiple words per 
line separated by spaces an exception is immediately thrown from 
std.format.


Thanks,
Andrew


Re: anything that would provide support for zip filesystem?

2012-03-22 Thread Trass3r

I guess Tango provides something like that via the VFS stuff.

https://github.com/SiegeLord/Tango-D2 resp.
https://github.com/mtachrono/tango


anything that would provide support for zip filesystem?

2012-03-22 Thread Jay Norwood
In my work we have to access a big zip file with a lot of little 
xml files in it to look up various target specific attributes for 
a wide variety of targets.


Basically the zip file it is a zipped up directory hierarchy of 
zip files, and  in processing it the hierarchy has meaning.


I see in the D library reference that we have the start of some 
zip processing, and we also have some url support in 
std.net.curl,  but is there anything provided that allows you to 
access files in a zip archive by url?


This description of java zipfilesystemprovider looks like it 
provides a solution.


http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/zipfilesystemprovider.html

While writing this I was also thinking about  D imports, and 
would it be useful feature if D could import modules using a zip 
url.


Re: Vector operations optimization.

2012-03-22 Thread Trass3r
What is the status at the moment? What compiler and with which compiler  
flags I should use to achieve maximum performance?


In general gdc or ldc. Not sure how good vectorization is though, esp.  
auto-vectorization.
On the other hand the so called vector operations like a[] = b[] + c[];  
are lowered to hand-written SSE assembly even in dmd.


Re: Regarding writefln formatting

2012-03-22 Thread Kenji Hara

On Wednesday, 21 March 2012 at 01:26:23 UTC, bearophile wrote:

import std.stdio;
void main() {
 auto mat = [[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]];

 writefln("%(%(%d %)\n%)", mat);
 writeln();

 writefln("[%(%(%d %)\n%)]", mat);
 writeln();

 writefln("[%([%(%d %)]\n%)]", mat);
 writeln();
}

Prints:

1 2 3
4 5 6
7 8 9

[1 2 3
4 5 6
7 8 9]

[[1 2 3]
[4 5 6]
[7 8 9]


Do you know why the last closed square bracket is missing?


You can use %| format specifier to specify element separator.

(It was proposed in 
https://github.com/D-Programming-Language/phobos/pull/298 .

It is not yet documented, but already merged in Phobos.)

import std.stdio;
void main() {
 auto mat = [[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]];

 writefln("[%([%(%d %)]%|\n%)]", mat);
 // specify "\n" as a separator
}

Prints:

[[1 2 3]
[4 5 6]
[7 8 9]]