Re: What do you think about using Chat GPT to create functions in D?

2023-04-03 Thread cgenie via Digitalmars-d-learn
I once tested ChatGPT to generate Common Lisp code (a simple 
Redis client). They had a warning that this is not a code 
generation tool nevertheless it tried to do so. All the code it 
had was incorrect: it imported foreign libraries that didn't 
exist and when I complained about it, it apologized and then 
proposed other libraries that didn't exist. Overall I had the 
impression that I'm working with some dumb person who pasted me 
stuff from stackoverflow that seemed to solve my issue.


Let me mention that I once asked ChatGPT what is 11 plus 11 and 
when it responded 22, I told it that it's wrong, it should be 30. 
It apologized and said that I'm right, it's 30. I tried to do the 
same experiment again after one day to show off my friends, but 
it became more resilient to such suggestions.


Re: foreach (i; taskPool.parallel(0..2_000_000)

2023-04-03 Thread Steven Schveighoffer via Digitalmars-d-learn

On 4/3/23 7:22 PM, Paul wrote:

```d
// Timed main() 
vvv

void main(string[] args) {
auto progStartTime = MonoTime.currTime;
//-
 string filename = args.length > 1 ? args[1] : "aoc2215a.data";
 CommPair[] commPair;
 ulong x,y;

 // read file that has data sets in the form of x,y coordinate pairs
 // for each sensor-beacon pair.  Create on array of structs to hold
 // this information.
 loadFileDataIntoArrayOfStructs(commPair, filename);

 foreach(int lineOfInterest;parallel(iota(0,4_000_001))) {
     Span[] span; // A section of line-of-interest coordinates where 
no other beacons are present.

     const spanReserve = span.reserve(50);
     createSpansOfNoBeacons(lineOfInterest,commPair,span);

     // if spans overlap, combine them into a single span and mark
     // the other spans !inUse.
     combineOverlappingSpans(span);

     // look for a line that doesn't have 4,000,001 locations 
accounted for

     if(beaconFreeLocations(span) < 4_000_001) {

     // find the location that is not accounted for
     foreach(ulong i;0..4_000_000) {
     bool found = false;
     foreach(sp;span) {
     if(i >= sp.xLow && i <= sp.xHigh) {
     found = true;
     break;
     }
     }
     if(!found) {
     x = i; y = lineOfInterest;
     break;
     }
     }
     }
 }
writeln(x," ",y);

```


So I just quoted your main loop.

I am assuming that this O(n^2) algorithm doesn't actually run for all 
iterations, because that wouldn't be feasible (16 trillion iterations is 
a lot).


This means that I'm assuming a lot of cases do not run the second loop. 
Everything you do besides prune the second loop is mostly allocating an 
array of `Span` types. This means most of the parallel loops are 
allocating, and doing nothing else. As I said earlier, allocations need 
a global lock of the GC.


What you need to do probably, is to avoid these allocations per loop.

The easiest thing I can think of is to store the Span array as a static 
array of the largest array you need (i.e. the length of `commPair`), and 
then slice it instead of appending.


So what you need is inside `createSpansOfNoBeacons`, take as a reference 
a `ref Span[MAX_SPANS]`, and have it return a `Span[]` that is a slice 
of that which was "alocated".


See if this helps.

FWIW, I did the AoC 2022 as well, and I never needed parallel execution. 
Looking at my solution comment in reddit, this one I sort of punted by 
knowing I could exit as soon as the answer is found (my solution runs in 
2.5s on my input). But I recommend (once you are done), reading this 
post, it is a really cool way to look at it:


https://www.reddit.com/r/adventofcode/comments/zmcn64/2022_day_15_solutions/j0hl19a/?context=8=9

-Steve


What do you think about using Chat GPT to create functions in D?

2023-04-03 Thread Marcone via Digitalmars-d-learn
What do you think about using Chat GPT to create functions in D? 
I asked Chat-GPT to create a function in D that was similar to 
the rsplit() function in Python. It returned this code to me:



import std.algorithm;
import std.array;
import std.string;

string[] rsplit(string input, string separator, size_t maxsplit = 
size_t.max) {

auto splitter = splitter(input, separator, SplitterFlag.all);
auto results = splitter.array;
if (maxsplit < results.length - 1)
results = results[results.length - maxsplit - 1 .. $];
return results;
}



Re: foreach (i; taskPool.parallel(0..2_000_000)

2023-04-03 Thread Paul via Digitalmars-d-learn
On Monday, 3 April 2023 at 23:13:58 UTC, Steven Schveighoffer 
wrote:


Yeah, please post.

```d
module aoc2215b2;

import std.stdio;
import std.file: readText;
import std.conv: to;
import std.math: abs;
import std.traits;
import std.parallelism;
import std.range;
import core.time: MonoTime;

// Timed main() 
vvv

void main(string[] args) {
auto progStartTime = MonoTime.currTime;
//-
string filename = args.length > 1 ? args[1] : "aoc2215a.data";
CommPair[] commPair;
ulong x,y;

	// read file that has data sets in the form of x,y coordinate 
pairs
	// for each sensor-beacon pair.  Create on array of structs to 
hold

// this information.
loadFileDataIntoArrayOfStructs(commPair, filename);

foreach(int lineOfInterest;parallel(iota(0,4_000_001))) {
		Span[] span; // A section of line-of-interest coordinates where 
no other beacons are present.

const spanReserve = span.reserve(50);
createSpansOfNoBeacons(lineOfInterest,commPair,span);

// if spans overlap, combine them into a single span and mark
// the other spans !inUse.
combineOverlappingSpans(span);

		// look for a line that doesn't have 4,000,001 locations 
accounted for

if(beaconFreeLocations(span) < 4_000_001) {

// find the location that is not accounted for
foreach(ulong i;0..4_000_000) {
bool found = false;
foreach(sp;span) {
if(i >= sp.xLow && i <= sp.xHigh) {
found = true;
break;
}
}
if(!found) {
x = i; y = lineOfInterest;
break;
}
}
}
}
writeln(x," ",y);

//-
auto progEndTime = MonoTime.currTime;
writeln(progEndTime - progStartTime);
}
// Timed main() 
^^^




struct CommPair {
int sx,sy,bx,by;
int manhattanDistance;
}

void loadFileDataIntoArrayOfStructs(ref CommPair[] commPair, 
string filename) {

import std.regex;
auto s = readText(filename);
	auto ctr = ctRegex!(`x=(-*\d+), y=(-*\d+):.*x=(-*\d+), 
y=(-*\d+)`);

CommPair cptemp;
foreach (c; matchAll(s, ctr)) {
cptemp.sx = to!int(c[1]);
cptemp.sy = to!int(c[2]);
cptemp.bx = to!int(c[3]);
cptemp.by = to!int(c[4]);
		cptemp.manhattanDistance = abs(cptemp.sx-cptemp.bx) + 
abs(cptemp.sy-cptemp.by);

commPair ~= cptemp;
}
}

struct Span {
int xLow, xHigh;
bool inUse = true;
}

void createSpansOfNoBeacons(int lineOfInterest, CommPair[] 
commPair,ref Span[] span) {

foreach(size_t i,cp;commPair) {
int distanceToLineOfInterest = abs(cp.sy - lineOfInterest);
if(cp.manhattanDistance >= distanceToLineOfInterest) {
			int xLow  = cp.sx - (cp.manhattanDistance - 
distanceToLineOfInterest);
			int xHigh = cp.sx + (cp.manhattanDistance - 
distanceToLineOfInterest);

span ~= Span(xLow,xHigh);
}
}
}

void combineOverlappingSpans(ref Span[] span) {
bool combinedSpansThisCycle = true;
while(combinedSpansThisCycle) {
combinedSpansThisCycle = false;
for(size_t i=0; i < span.length-1; i++) {
if(!span[i].inUse) continue;

for(size_t j=i+1; j < span.length; j++) {
if(!span[j].inUse) continue;

// if one span overlaps with the other, combine them into one 
span
if(spanIContainedInSpanJ(span[i],span[j]) || 
spanJContainedInSpanI(span[i],span[j])) {
	span[i].xLow  = span[i].xLow  < span[j].xLow  ? span[i].xLow 
 : span[j].xLow;
	span[i].xHigh = span[i].xHigh > span[j].xHigh ? 
span[i].xHigh : span[j].xHigh;

span[j].inUse = false;
combinedSpansThisCycle = true;

// after combining two spans, perform 
bounds checking
// 15 part b limits the search between 
0 and 4,000,000
	span[i].xLow  = span[i].xLow  < 0 ? 0 : 
span[i].xLow;
	span[i].xHigh = span[i].xHigh > 4_000_000 ? 4_000_000 : 
span[i].xHigh;

 

Re: foreach (i; taskPool.parallel(0..2_000_000)

2023-04-03 Thread Steven Schveighoffer via Digitalmars-d-learn

On 4/3/23 6:56 PM, Paul wrote:

On Monday, 3 April 2023 at 22:24:18 UTC, Steven Schveighoffer wrote:



If your `foreach` body takes a global lock (like `writeln(i);`), 
then it's not going to run any faster (probably slower actually).

**Ok I did have some debug writelns I commented out.**


And did it help?

**No**

My program is about 140 lines Steven.  Its just one of the Advent of 
Code challenges.  Could I past the whole program here and see what you 
think?


Yeah, please post.

-Steve


Re: foreach (i; taskPool.parallel(0..2_000_000)

2023-04-03 Thread Paul via Digitalmars-d-learn
On Monday, 3 April 2023 at 22:24:18 UTC, Steven Schveighoffer 
wrote:




If your `foreach` body takes a global lock (like 
`writeln(i);`), then it's not going to run any faster 
(probably slower actually).

**Ok I did have some debug writelns I commented out.**


And did it help?

**No**

My program is about 140 lines Steven.  Its just one of the Advent 
of Code challenges.  Could I past the whole program here and see 
what you think?


Thanks for your assistance...much appreciated.





Re: foreach (i; taskPool.parallel(0..2_000_000)

2023-04-03 Thread Steven Schveighoffer via Digitalmars-d-learn

On 4/3/23 6:02 PM, Paul wrote:

On Sunday, 2 April 2023 at 15:32:05 UTC, Steven Schveighoffer wrote:



It's important to note that parallel doesn't iterate the range in 
parallel, it just runs the body in parallel limited by your CPU count.

**?!?**


So for example, if you have:

```d
foreach(i; iota(0, 2_000_000).parallel)
{
   runExpensiveTask(i);
}
```

The foreach is run on the main thread, gets a `0`, then hands off to a 
task thread `runExpensiveTask(0)`. Then it gets a `1`, and hands off to 
a task thread `runExpensiveTask(1)`, etc. The iteration is not 
expensive, and is not done in parallel.


On the other hand, what you *shouldn't* do is:

```d
foreach(i; iota(0, 2_000_000).map!(x => runExpensiveTask(x)).parallel)
{
}
```

as this will run the expensive task *before* running any tasks.



If your `foreach` body takes a global lock (like `writeln(i);`), then 
it's not going to run any faster (probably slower actually).

**Ok I did have some debug writelns I commented out.**


And did it help? Another thing that takes a global lock is memory 
allocation.



Also make sure you have more than one logical CPU.

**I have 8.**


It's dependent on the work being done, but you should see a roughly 8x 
speedup as long as the overhead of distributing tasks is not significant 
compared to the work being done.


-Steve


Re: foreach (i; taskPool.parallel(0..2_000_000)

2023-04-03 Thread Paul via Digitalmars-d-learn
On Sunday, 2 April 2023 at 15:32:05 UTC, Steven Schveighoffer 
wrote:




It's important to note that parallel doesn't iterate the range 
in parallel, it just runs the body in parallel limited by your 
CPU count.

**?!?**

If your `foreach` body takes a global lock (like 
`writeln(i);`), then it's not going to run any faster (probably 
slower actually).

**Ok I did have some debug writelns I commented out.**

If you can disclose more about what you are trying to do, it 
would be helpful.
**This seems like it would be a lot of code and explaining but 
let me think about how to summarize.**



Also make sure you have more than one logical CPU.

**I have 8.**




Re: Is it possible to make a library (dll/so) from separated .d files?

2023-04-03 Thread dog2002 via Digitalmars-d-learn

On Monday, 3 April 2023 at 11:29:12 UTC, Hipreme wrote:

On Monday, 3 April 2023 at 09:08:42 UTC, dog2002 wrote:

Hello. The title sounds weird, but I try to explain it better.

In Unreal Engine and Unity, source code files don't use main() 
and other important functions. They have only a class. Like 
this:


```
class SomeClass: someInterface
{
AFunctionFromTheInterface1()
{

}

AFunctionFromTheInterface2()
{

}
}
```

And then all the source code files will be compiled into a 
single .dll/.so library, so the game engine can use one in a 
game.


I don't know what compiler does Unreal Engine use, but it uses 
C++.



https://github.com/MrcSnm/HipremeEngine/blob/66618c7783d62107bcaad393d5af5b86c9387b34/api/source/hip/api/package.d#L58



Thank you, looks almost what I need!


Re: Is it possible to make a library (dll/so) from separated .d files?

2023-04-03 Thread Hipreme via Digitalmars-d-learn

On Monday, 3 April 2023 at 09:08:42 UTC, dog2002 wrote:

Hello. The title sounds weird, but I try to explain it better.

In Unreal Engine and Unity, source code files don't use main() 
and other important functions. They have only a class. Like 
this:


```
class SomeClass: someInterface
{
AFunctionFromTheInterface1()
{

}

AFunctionFromTheInterface2()
{

}
}
```

And then all the source code files will be compiled into a 
single .dll/.so library, so the game engine can use one in a 
game.


I don't know what compiler does Unreal Engine use, but it uses 
C++.


Is it possible to do so in D?



Yes, actually, this is the very same approach I've done for my 
engine, since holding the main function I can take care of boring 
platform details.


For doing that you'll need some way to make your main program 
know about your class. The way I do that is by defining another 
entry function which is always defined enemy you have a game 
project.



You can take a look at 
https://github.com/MrcSnm/HipremeEngine/blob/66618c7783d62107bcaad393d5af5b86c9387b34/api/source/hip/api/package.d#L58



The user uses that engine mixin template, which will add the 
engine entry point to your game script, after that, whenever my 
engine loads the Dll, it will know what function to call and what 
scene it should spawn


Re: Is it possible to make a library (dll/so) from separated .d files?

2023-04-03 Thread dog2002 via Digitalmars-d-learn

On Monday, 3 April 2023 at 09:08:42 UTC, dog2002 wrote:

Hello. The title sounds weird, but I try to explain it better.

In Unreal Engine and Unity, source code files don't use main() 
and other important functions. They have only a class. Like 
this:


```
class SomeClass: someInterface
{
AFunctionFromTheInterface1()
{

}

AFunctionFromTheInterface2()
{

}
}
```

And then all the source code files will be compiled into a 
single .dll/.so library, so the game engine can use one in a 
game.


I don't know what compiler does Unreal Engine use, but it uses 
C++.


Is it possible to do so in D?


There are examples of code here 
https://docs.unrealengine.com/5.1/en-US/unreal-engine-for-unity-developers/


Is it possible to make a library (dll/so) from separated .d files?

2023-04-03 Thread dog2002 via Digitalmars-d-learn

Hello. The title sounds weird, but I try to explain it better.

In Unreal Engine and Unity, source code files don't use main() 
and other important functions. They have only a class. Like this:


```
class SomeClass: someInterface
{
AFunctionFromTheInterface1()
{

}

AFunctionFromTheInterface2()
{

}
}
```

And then all the source code files will be compiled into a single 
.dll/.so library, so the game engine can use one in a game.


I don't know what compiler does Unreal Engine use, but it uses 
C++.


Is it possible to do so in D?


Re: short guide on getting started with D

2023-04-03 Thread Salih Dincer via Digitalmars-d-learn

On Monday, 3 April 2023 at 07:29:01 UTC, cgenie wrote:

Thanks, you got my attention...

It has come to my attention that you talked about this:

https://mesonbuild.com/Dlang-module.html

SDB@79


short guide on getting started with D

2023-04-03 Thread cgenie via Digitalmars-d-learn

Hello,

I created a short guide on getting started with D: 
https://blog.mmksoft.uk/#A%20short%20guide%20on%20getting%20started%20with%20D%20programming


This is because I recently I started to explore the language and, 
having read the forum, I see DUB being discouraged quite often.


I would appreciate any remarks.

Best,
Przemek