In this episode I talk with Brian Alexakis about his role and what exactly it means to be a Knowledge Marketer. We also talk about the similarities between FireMonkey and some web development tools, and discuss virtual reality and what it holds in store for our near future. You can follow Brian Alexakis and his tutorials and C++ examples on blog.appmethod.com or on Twitter as @IoTBrian.
Here are some links that Brian mentioned during the podcast:
To follow-up with my Parallel For Loops skill sprint last week (lots of good resources there), today I did a deep dive into the Parallel Programming Library (PPL) including Futures, Tasks and custom thread pools.
And you can download my Tasks and Futures sample to see how to create, start, check and cancel both Tasks and Futures. Check out the C++ Parallel Examples to see how to use Futures and the PPL in C++.
Until the replay of my Skill Sprint is posted, you can watch this video from Allen Bauer covering the Parallel Programming Library Architecture from CodeRage 9.
Be sure to sign up for and check out the other Skill Sprints.
This is some code I wrote a long time ago as part of the TDAG mailing list. It calculates Factorials, the number of Combinations and Permutations. This is the mathematics, it calculates the number of Combinations and Permutations, it doesn’t create each different Combination and Permutation.
They take Cardinals as parameters because it doesn’t make sense to accept negative numbers. Keep in mind that factorials get big really, really quickly, so if you use too large of numbers then it will overflow even the Int64.
unit FactCombPerm;
interface
// Parameters are Cardinal because Int64 isn't Ordinal
// Besides, Factorial(100) will overflow Int64 already.
function Factorial(const ANumber: Cardinal): Int64; overload;
function Factorial(const ANumber: Cardinal;
const AStop: Cardinal): Int64; overload;
function CombinationUnOpt(const ACount, AChoose: Cardinal): Int64;
function Combination(const ACount, AChoose: Cardinal): Int64;
function Permutation(const ACount, AChoose: Cardinal): Int64;
implementation
function Factorial(const ANumber: Cardinal): Int64; overload;
// n! = n * (n-1) . . . n(2)
var
lCtr: Cardinal;
begin
Result := 1;
for lCtr := ANumber downto 2 do
Result := Result * lCtr;
end;
function Factorial(const ANumber: Cardinal;
const AStop: Cardinal): Int64; overload;
// Factorial with a stop point (needed in the optimized Combination routine
// if no AStop is specified then is the same as Factorial
var
lCtr: Cardinal;
begin
Result := 1;
if ANumber >= AStop then
for lCtr := ANumber downto AStop do
Result := Result * lCtr;
end;
function CombinationUnOpt(const ACount, AChoose: Cardinal): Int64;
// n!
// n_C_k = ----------
// k!(n - k)!
begin
if AChoose < ACount then
Result := Factorial(ACount)
div (Factorial(AChoose) * Factorial(ACount - AChoose))
else
Result := 0;
end;
function Combination(const ACount, AChoose: Cardinal): Int64;
// n!
// n_C_k = ----------
// k!(n - k)!
// with optimizations even!
begin
if AChoose < ACount then
Result := Factorial(ACount, succ(ACount - AChoose))
div (Factorial(AChoose))
else
Result := 0;
end;
function Permutation(const ACount, AChoose: Cardinal): Int64;
// n!
// n_P_k = --------
// (n - k)!
begin
if AChoose < ACount then
Result := Factorial(ACount)
div Factorial(ACount - AChoose)
else
Result := 0;
end;
end.
My original optimization had a bug in it, but Bryan Mayland fixed it for me. This is old code, and there are probably some options to optimize it better. I’d love input, suggestions, etc.
I hope you are all as excited about DelphiWeek as we are. On Wednesday we have CodeBattles scheduled. These are a fun way to show off your programming skills. We all use a Google, DocWiki and Stack Overflow during programming. The coding challenge isn’t a trivia competition, but an opportunity for you to show off your problem solving skills and use some of the latest features in XE7.
It will take place Wednesday 11-Feb-2015 from 8 AM to 10 AM PST. All coding will be done online during this time window via GoToWebinar.
The scoring is a combination of points based on features implemented and public voting. David I. is planning to make plaques to send the winner. I think it would be great to hang a plaque in the office with the winner’s names too.
If you want to take part in the CodeBattle, complete this short survey with your information. We are considering working with both teams and individuals, if we get enough sign ups for both. So far we already have a few individuals. It is open to all Delphi developers everywhere. Even if you are not comfortable with English, we can chat via Google Translate.
Parallel For Loops are a hassle-free way to supercharge your program with the Parallel Programming Library. The syntax is similar to the standard For loop, with the advantage of each iteration running on in a different task on the thread pool. This allows multiple iterations to run at the same time, taking advantage of the multi-core and hyper-threaded architecture common on laptops, desktops and mobile devices today.
Update: I was chatting with Allen Bauer today and he mentioned that while you technically can use Queue and Synchronize from within a Parallel For loop, he wouldn’t recommend it because it will dramatically reduce the speed of the loop. It is still faster than a linear loop, but not as fast as it could be. I’ll leave these examples here, but keep that in mind when optimizing your parallel code.
Here is the syntax in Object Pascal. The stride is the first parameter is it is optional. It controls how the iterations are grouped when being sent to the CPUs. Min and Max are your usual start and stop range for the loop. The last parameter is an anonymous method that represents the code to be executed on each iterations. It takes an Index parameter that is either an Integer or Int64 that provides the value of the current iteration.
TParallel.For(Stride, Min, Max, procedure (Idx: Integer)
begin
if IsPrime(Idx) then
begin
TInterlocked.Increment (Tot);
TThread.Queue(TThread.CurrentThread, procedure
begin
Memo1.Lines.Add(Idx.ToString);
end);
end;
end);
Here is the C++ code syntax. It takes a event instead of an anonymous method.
Update: Keep in mind that a Parallel For loop isn’t always the best performance option, but it is really easy to use. Check out Stefan Glienke’s Stack Overflow answer for an alternative using the PPL TTask for even better performance.
How are you Delphi programming skills? Want to visit Fortaleza, Brazil? I’m sure it is especially nice if you are in the northern hemisphere right now since Brazil is enjoying summer while we are stuck in winter.
Next week Hack’n Fortes is hosting a Delphi hackathon / programming marathon. Registration is closed, but you can always head down to watch. If Brazil is a little far away, we have an online Code Battle planned for Wednesday the 15th as part of Delphi Week 2015! #DelphiWeek
I’ve been playing with Raize Software‘s new Radiant Shapes components this week. These are the brand new primitive shape component set for FireMonkey on all platforms: Windows, OS X, iOS and Android. I’ve been a long time fan of Raize Components because of their attention to detail and high quality. Radiant Shapes continues this tradition.
Radiant Shapes is made up of 35 reusable shape controls that are all pretty flexible. If you caught Ray Konopka’s RAD In Action: Seeing is Believing on Data Visualization then you have a pretty good idea the importance of using primitive shapes like these to communicate useful information to your users, especially in mobile development.
All of the shapes include useful design time menus to make common changes quickly and easily. You can probably get away without using the Object Inspector for a lot of your common tasks. They also have various customizations that make them very flexible.
One thing that is interesting is they introduce the idea of a TRadiantDimension they allows you to specify some of the sizes as either absolute pixels, or as a scale factor. This gives great flexibility in how they behave when resized.
Ray Konopka introduced the Radiant Shapes during CodeRage 9 with a couple great sessions. You can catch the replay for both Object Pascal and C++.
I really like the TRadiantGear component, so I decided to play with it in detail. You can specify the number of cogs (teeth), their size (as a Radiant Dimension) and the size and visibility of the hole. Just like all the other shapes, they handle hit tests correctly, so at runtime, you can click between the cogs of the gear and it doesn’t produce an onClick event.
Just for fun I put down three gears and used LiveBindings to connect a TTrackBar.Value to their rotation. A little math in the OnAssigningValue event and I had all the gears rotating in unison. The fact that the gears stayed synced up, and the teeth meshed perfectly was really impressive.
procedure TForm4.RotateGearBigAssigningValue(Sender: TObject;
AssignValueRec: TBindingAssignValueRec; var Value: TValue;
var Handled: Boolean);
begin
Value := TValue.From(-1 * (Value.AsExtended / 2 + 18));
end;
procedure TForm4.RotateGearRightAssigningValue(Sender: TObject;
AssignValueRec: TBindingAssignValueRec; var Value: TValue;
var Handled: Boolean);
begin
Value := TValue.From(-1 * (Value.AsExtended + 18));
end;
18 is the offset for the gears (360° / 10 cogs / 2 (half offset) = 18) and the 2 comes from the big gear being twice as big (20 cogs), then the -1 is so they rotate the opposite direction.
Overall I am impressed with the Radiant Shapes. Something I would like to see include a polygon component where I can specify the number of sizes. You can do that with the star and gear, but a flexible polygon would be nice. Also, the shapes can be rotated with the rotation property, but it would be cool if there was a way to rotate it in the designer too. That might be a big undertaking though.
An interview with John Thomas “JT”, Senior Director of Product Management over RAD Studio and Delphi with Embarcadero Technologies.This episode was recorded almost a year and a half ago, right after the XE5 release added Android support, but is still a relevant conversation today.
It is 2 weeks from our last episode, so we are going to try and keep it at an episode every 2 weeks for now and see how that goes. Also we are moving to Soundcloud for audio hosting.