Re: Mir Slice.shape is not consistent with the actual array shape

2020-05-24 Thread Pavel Shkadzko via Digitalmars-d-learn

On Sunday, 24 May 2020 at 15:24:14 UTC, 9il wrote:

On Sunday, 24 May 2020 at 14:17:33 UTC, Pavel Shkadzko wrote:

[...]


BTW, the code example above doesn't compiles.

OT:
Instead of


[...]


you can generate the same common D array using Mir:

auto a = [2, 2, 4].iota!int(1).ndarray;


I posted in a rush. There should be "auto arrSlice = a.sliced;" 
and "writeln(a.getShape);".


Re: Mir Slice.shape is not consistent with the actual array shape

2020-05-24 Thread Pavel Shkadzko via Digitalmars-d-learn

On Sunday, 24 May 2020 at 14:35:33 UTC, jmh530 wrote:

On Sunday, 24 May 2020 at 14:21:26 UTC, Pavel Shkadzko wrote:

[snip]

Sorry for the typo. It should be "auto arrSlice = a.sliced;"


Try using fuse

/+dub.sdl:
dependency "mir-algorithm" version="*"
+/
import std.stdio;
import std.conv;
import std.array: array;
import std.range: chunks;
import mir.ndslice;

int[] getShape(T : int)(T obj, int[] dims = null)
{
return dims;
}

// return arr shape
int[] getShape(T)(T obj, int[] dims = null)
{
dims ~= obj.length.to!int;
return getShape!(typeof(obj[0]))(obj[0], dims);
}

void main() {
int[] arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
15, 16];

int[][][] a = arr.chunks(4).array.chunks(2).array;

int err;
writeln(arr);
writeln(a.shape(err));

auto aSlice = a.fuse;
writeln(aSlice);
writeln(aSlice.shape);

}


Yes, this works. It doesn't explain why sliced behaves like this 
though. Also, isn't sliced the default way of converting D arrays 
to Mir arrays? Above all arr.fuse.field flattens the array. So, 
fuse works as D join if combined with field but as slice 
allocator is used on a D array...


Re: Mir Slice.shape is not consistent with the actual array shape

2020-05-24 Thread Pavel Shkadzko via Digitalmars-d-learn

On Sunday, 24 May 2020 at 14:21:26 UTC, Pavel Shkadzko wrote:

On Sunday, 24 May 2020 at 14:17:33 UTC, Pavel Shkadzko wrote:

I am confused by the return value of Mir shape.
Consider the following example.

[...]


Sorry for the typo. It should be "auto arrSlice = a.sliced;"


And another typo "writeln(arr.getShape);", too rushy.


Re: Mir Slice.shape is not consistent with the actual array shape

2020-05-24 Thread Pavel Shkadzko via Digitalmars-d-learn

On Sunday, 24 May 2020 at 14:17:33 UTC, Pavel Shkadzko wrote:

I am confused by the return value of Mir shape.
Consider the following example.

[...]


Sorry for the typo. It should be "auto arrSlice = a.sliced;"


Mir Slice.shape is not consistent with the actual array shape

2020-05-24 Thread Pavel Shkadzko via Digitalmars-d-learn

I am confused by the return value of Mir shape.
Consider the following example.

///
import std.stdio;
import std.conv;
import std.array: array;
import std.range: chunks;
import mir.ndslice;

int[] getShape(T : int)(T obj, int[] dims = null)
{
return dims;
}

// return arr shape
int[] getShape(T)(T obj, int[] dims = null)
{
dims ~= obj.length.to!int;
return getShape!(typeof(obj[0]))(obj[0], dims);
}

void main() {
int[] arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
15, 16];

int[][][] a = arr.chunks(4).array.chunks(2).array;

writeln(arr);
writeln(arr.shape);

auto arrSlice = arr.sliced;
writeln(arrSlice);
writeln(arrSlice.shape);

}
///

[[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 
16]]]

[2, 2, 4] <-- correct shape
[[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 
16]]]

[2] <-- which shape is that?

I would expect sliced to create a Slice with the same dims. Well, 
sliced returns a shell over the array, but why does it return its 
own shape instead of the shape of the array it provides view 
into? This makes it even more confusing once you print both 
representations.

What's the rationale here?


Re: How to flatten N-dimensional array?

2020-05-24 Thread Pavel Shkadzko via Digitalmars-d-learn

On Sunday, 24 May 2020 at 11:21:00 UTC, 9il wrote:

On Saturday, 23 May 2020 at 18:15:32 UTC, Pavel Shkadzko wrote:

[...]


If the common nd-array isn't jugged (a parallelotop), you can 
use fuse function.


--
/+dub.sdl:
dependency "mir-algorithm" version="~>3.8.12"
+/
import std.stdio: writeln;
import mir.ndslice;

void main() {
auto arr =
[[[0, 1, 2, 3, 4],
  [5, 6, 7, 8, 9]],
 [[10, 11, 12, 13, 14],
  [15, 16, 17, 18, 19]],
 [[20, 21, 22, 23, 24],
  [25, 26, 27, 28, 29]]];
auto flatten = arr.fuse.field;

static assert(is(typeof(flatten) == int[]));
assert(flatten == 30.iota);
}
--

It performs exactly one allocation.


Yep, I know about Mir fuse. I was more wondering about the 
absence of flatten in Phobos. But on dlang forum some people 
claimed that when it comes to anything multidimensional, use Mir.


Re: How to flatten N-dimensional array?

2020-05-24 Thread Pavel Shkadzko via Digitalmars-d-learn

On Saturday, 23 May 2020 at 19:59:30 UTC, Ali Çehreli wrote:
On 5/23/20 11:15 AM, Pavel Shkadzko wrote:> I have tried to 
implement a simple flatten function for multidimensional


[...]


Thank you, I was lacking practical examples for templates with 
"if" constructs, ehh.


How to flatten N-dimensional array?

2020-05-23 Thread Pavel Shkadzko via Digitalmars-d-learn
I have tried to implement a simple flatten function for 
multidimensional arrays with recursive templates but got stuck. 
Then I googled a little and stumped into complex 
https://rosettacode.org/wiki/Flatten_a_list#D implementation 
which requires your arrays to be either TreeList or Algebraic. 
That is, it does not work out-of-the-box on something like 
int[][][].


I'd like to clarify a couple of questions first.

How come Phobos doesn't have "flatten" function for arrays?

Is there an implementation of flatten that works out-of-the-box 
on N-dim arrays outside of Phobos? Excluding "flattened" from 
mir.ndslice since it works on Slices.


Re: How does a free list work?

2020-05-12 Thread Pavel Shkadzko via Digitalmars-d-learn

On Saturday, 9 May 2020 at 22:48:46 UTC, Simen Kjærås wrote:

On Saturday, 9 May 2020 at 19:54:44 UTC, Pavel Shkadzko wrote:

[...]


The GC keeps a list of roots - that is, objects that are known 
to be active and should not be collected. The static freelist 
is one of those - since it's static it should of course never 
be collected.


[...]


Thank you for such a detailed answer!


How does a free list work?

2020-05-09 Thread Pavel Shkadzko via Digitalmars-d-learn
I have been reading about memory management in D on 
https://wiki.dlang.org/Memory_Management and found an example of 
a free list (pattern?): "Free lists are a great way to accelerate 
access to a frequently allocated and discarded type.".


Here is the example of free list:
---
class Foo
{
static Foo freelist; // start of free list
Foo next; // for use by FooFreeList

static Foo allocate()
{
Foo f;

if (freelist)
{
f = freelist;
freelist = f.next;
}
else
f = new Foo();
return f;
}

static void deallocate(Foo f)
{
f.next = freelist;
freelist = f;
}
}
---

Do I understand correctly that by switching between static and 
non-static Foo we keep the object from being garbage collected by 
the GC? So in a situation when I need to create an object and 
then discard it, I can implement this pattern to use memory more 
efficiently.


Also, it's a little strange that since both f and freelist are 
null we are basically doing null = null in first if condition.




Re: How to sort 2D Slice along 0 axis in mir.ndslice ?

2020-03-11 Thread Pavel Shkadzko via Digitalmars-d-learn

On Wednesday, 11 March 2020 at 06:12:55 UTC, 9il wrote:

On Wednesday, 11 March 2020 at 00:24:13 UTC, jmh530 wrote:

[...]


Almost the same, just fixed import for `each` and a bit polished

/+dub.sdl:
dependency "mir-algorithm" version="~>3.7.18"
+/
import mir.ndslice;
import mir.ndslice.sorting;
import mir.algorithm.iteration: each;

void main() {
auto m = [[1, -1, 3, 2],
  [0, -2, 3, 1]].fuse;
m.byDim!0.each!sort;

import std.stdio;
m.byDim!0.each!writeln;
}


Great, thanks guys!


Re: Compilation error: undefined reference to 'cblas_dgemv' / 'cblas_dger' / 'cblas_dgemm'

2020-03-10 Thread Pavel Shkadzko via Digitalmars-d-learn

On Monday, 13 January 2020 at 20:47:07 UTC, p.shkadzko wrote:

On Sunday, 12 January 2020 at 13:07:33 UTC, dnsmt wrote:

On Saturday, 11 January 2020 at 16:45:22 UTC, p.shkadzko wrote:
I am trying to run example code from 
https://tour.dlang.org/tour/en/dub/lubeck


...

This is Linux Manjaro with openblas package installed.


The Lubeck library depends on CBLAS, but the openblas package 
in the Arch repository is compiled without CBLAS. You can see 
that here (note the NO_CBLAS=1 parameter): 
https://git.archlinux.org/svntogit/community.git/tree/trunk/PKGBUILD?h=packages/openblas


Try installing the cblas package: 
https://www.archlinux.org/packages/extra/x86_64/cblas/


yeah, so I thought too but I have CBLAS installed as a distro 
package. I also have OpenBLAS installed instead of BLAS because 
BLAS was removed since it conflicts with OpenBLAS ¯\_(ツ)_/¯


Figured it out by using mir-blas and linking it with openBlas on 
Linux.

You'll need openBlas and cblas installed.
dub.json needs "subConfigurations": {"mir-blas": "twolib"}
Then you can import blas functions like so import mir.blas : gemm;




How to generate ddoc html?

2020-01-29 Thread Pavel Shkadzko via Digitalmars-d-learn
I've been skimming through https://dlang.org/spec/ddoc.html in 
order to understand how can one use ddoc to generate nice htmls. 
I tend to use markdown to log some daily work or copy down code 
examples. For learning purposes I wanted to try ddoc for this but 
could not find any information on ddoc.html page on how to 
actually generate the html page. I guessed that dmd should have a 
switch to generate docs and was correct: dmd mylogs.dd -D would 
generate mylogs.html.


I decided to generate the actuall ddoc.html by simply copying its 
contents into a separate file and calling dmd -D on it but the 
generated ddoc.thml file contained only the title and nothing 
else. I was then told on IRC that ddoc.dd lacks files with marco 
definitions. But where are these files? I suspect these are the 
many .ddoc files in https://github.com/dlang/dlang.org root dir:


https://github.com/tastyminerals/dlang.org/blob/master/macros.ddoc
https://github.com/dlang/dlang.org/blob/master/doc.ddoc
https://github.com/tastyminerals/dlang.org/blob/master/errorpage.ddoc

etc.

But how do I link them together then? Do I need to have some 
specific dir structure? I might be wrong but I couldn't find this 
information on ddoc.thml. Maybe the documentation page needs some 
improvement? I could add this information if only I knew how to 
do it in the first place :)


Re: books for learning D

2020-01-13 Thread Pavel Shkadzko via Digitalmars-d-learn

On Monday, 13 January 2020 at 10:28:48 UTC, mark wrote:

I'm just starting out learning D.

Andrei Alexandrescu's "The D Programming Language" is 10 years 
old, so is it still worth getting? (I don't know how much D has 
changed in 10 years.)


Start with "Programming in D" by Ali: 
http://ddili.org/ders/d.en/index.html
A good all-rounder book for any level, you can easily skip the 
chapters if you already know the domain. It starts slowly with a 
lot of detail (which I personally liked) but then gets a bit 
rushed and sketchy in the end still a **must-read** for anyone 
who comes from Python or any other "high level" language since a 
lot of things will be new to you.


After studying Ali's book you will be pretty much ready to code, 
in fact you'll be ready to code in roughly a week or so since D 
is such an easy language to pick up ;)


Then you can go straight to "D Cookbook" by Adam: 
https://dlang.org/blog/2016/08/04/the-origins-of-the-d-cookbook/
Don't start this book if you don't know D at least a little or if 
you're quite experienced with C++. I find that C++ devs can 
quickly jump to D. The book is basically a collection of various 
problems and solutions in D with nice explanations. I am still on 
it.


"The D Programming Language" book by Andrei: 
https://www.amazon.com/gp/product/0321635361/ref=as_li_qf_sp_asin_il_tl?ie=UTF8=1789=9325=0321635361=as2=dlang-20=BOLS7NQK6MXCZTMG
I really enjoy Anrei's style of writing but I think this book is 
mostly an good evening read that is -- it is more about the 
history and ideas behind D. Good for high level understanding of 
the language concepts. (Correct me if I am wrong because I 
haven't read it fully yet).


Finally, you have plenty of materials on dlang website:
https://dlang.org/comparison.html
https://dlang.org/articles/index.html



Re: How to organize a project with local scripts in subdirectories using dub?

2019-12-11 Thread Pavel Shkadzko via Digitalmars-d-learn

On Wednesday, 11 December 2019 at 16:06:30 UTC, drug wrote:


Add to dub.json:
"targetType": "executable"

and it will build executable. Or rename evaluator.d to app.d
It is confused that there is no app.d so it thinks that you 
build a library.


I added "targetType" and now everything works without renaming to 
the script to "app.d". Thank you!


Re: How to organize a project with local scripts in subdirectories using dub?

2019-12-11 Thread Pavel Shkadzko via Digitalmars-d-learn

On Wednesday, 11 December 2019 at 13:00:32 UTC, drug wrote:


I would restructure your
folders this way:
my_proj/
source/
script_1/runme_1.d
script_2/runme_2.d
evaluator.d

then using `dub --compiler=ldc2`


It works like this, yes. However, by default it builds a 
"library" while I want to run "evaluator" as a script which uses 
other scripts by calling their functions.


Doing:
dub --compiler=ldc2 --single evaluator.d // throws an error

source\evaluator.d(93,12): Error: module runme_1 is in file 
'runme_1.d' which cannot be read





How to organize a project with local scripts in subdirectories using dub?

2019-12-11 Thread Pavel Shkadzko via Digitalmars-d-learn

I have the following project structure.

my_proj/
script_1/runme_1.d
script_2/runme_2.d
evaluator.d

The project has a library dependency:

"dependencies": {
"mir": "~>3.2.0",
},

The "evaluator.d" script contains some general functions that 
convert dataset and pass it to "runme_1.d" and "runme_2.d" 
scripts the following way:


import mir.ndslice;

void main() {
import script_1.runme_1 : runTask1;
import script_2.runme_2 : runTask2;

mir_slice!(real*, 1LU, cast(mir_slice_kind)2)[]) dataset = 
convertDataset("data.txt");

runTask1(dataset);
//runTask2(dataset); //disable for now
}

So, basically what I need is to be able to run "./evaluator" and 
get the results by running the corresponding scripts.


I am trying to build the project the following way:

dub build --compiler=ldc2 --single evaluator.d

I get the following error:

Performing "debug" build using ldc2 for x86_64.
mir-core 1.0.2: target for configuration "library" is up to date.
mir-algorithm 3.7.2: target for configuration "default" is up to 
date.
mir-linux-kernel 1.0.1: target for configuration "library" is up 
to date.
mir-random 2.2.8: target for configuration "extended" is up to 
date.

survey ~master: building configuration "application"...
lld-link: error: undefined symbol: 
_D5runme_114runTask1FS3mir7ndslice5slice__T9mir_sliceTPeVmi2VEQBoQBnQBi14mir_slice_kindi2ZQBvZv

referenced by C:\Users\user\my_proj\evaluator.d:89
  .dub\obj\evaluator.obj:(_Dmain)

Error: linking with LLD failed
ldc2 failed with exit code 1.

How should I organize my project for this kind of setup?

(I am sorry for the absence of code formatting. I could not find 
any quick howto here)