ComputerScientist
174 subscribers
14 photos
3 files
206 links
▜ The Inventor

Stuff that inspire you to create.

See also: ▙ @LitMind
Download Telegram
Equivalence
- is a binary relation that is reflexive, symmetric and transitive; i.e. for any objects a, b, and c:

a = a, reflexive
if a = b then b = a, symmetric
if a = b and b = c then a = c, transitive
History of Windows
Windows 1.0, 2.0, and 3.11 were built as a simple 16-bit GUI layer over DOS. Microsoft began to remove dependencies on DOS in Windows 95, Windows 98, and Windows ME; though DOS was still present.

The separation was finally completely implemented, fully 32-bit, in Windows NT and 2000. Windows NT 4.0, Windows 2000, Windows XP, Windows Vista, Windows 7, and Windows Server are all based on the NT kernel.

New Technology
The NT Kernel is a collection of code that has an API consisting of thousands of (mostly undocumented) GUI functions. It also can emulate some DOS functionality. Microsoft also introduced many API wrappers, such as the MFCs (Microsoft Foundation Classes), COM (Component Object Model), and .NET technologies.

The most popular languages for use on Windows include Visual Basic/VB6 and C/C++, although C++ is quickly being replaced by the .NET platform, specifically C#.


Windows Architecture
The first layer: NTOSKRNL.EXE and HAL.DLL
NTOSKRNL.EXE provides some of the basic functionality of Windows but relies heavily on HAL.DLL. HAL stands for "Hardware Abstraction Layer", and is the portion of code that allows low-level mechanisms such as interrupts and BIOS communication to be handled independently.

The second layer: NTDLL.DLL and WIN32K.SYS
NTDLL.DLL contains a number of user-mode functions such as system call stubs and the RTL (run-time library) code collectively known as the (largely undocumented) "Native API". Much of the RTL code is shared between NTOSKRNL and NTDLL. WIN32K.SYS is a kernel-mode driver that implements the windowed GUI.

The third layer: Win32 API
Contains development libraries i.e. almost all the functions that a user will ever need to program in Windows. It is divided into 4 DLLs:

kernel32.DLL
Contains wrappers around lower-level NTDLL functions, NLS (National Language Support) and console handling

advapi32.DLL
Contains functions for registry and service handling.

gdi32.DLL
Contains functions for basic drawing, bitmap display and manipulation.

user32.DLL
Contains familiar Windows GUI implementations e.g. message boxes. It uses system calls implemented by WIN32K.SYS.

In addition to the 4 primary libraries in the Win32 API, there are a number of other important libraries that a Windows programmer should become familiar with:

MSVCRT.DLL
Contains implementations for C stdlib functions defined in common headers such as stdio.h, string.h, stdlib.h, etc.

WS2_32.DLL - The Winsock2 library
Contains the standard Berkeley socket API for communicating on the internet.

📚 Summary of: Windows Programming » Windows System Architecture
User Mode versus Kernel Mode
In Windows (and most OSs), there is a distinction between code that is running in user mode, and code that is running in kernel mode, because if all programs ran in kernel mode, they would be able to overwrite each others' memory and possibly bring down the entire system when they crashed.

This distinction has roots in lower levels; for example Intel CPUs have modes of operation called rings which specify the type of instructions and memory available to the running code:
• Ring 0 (kernel mode) full access to every resource, used by the Windows kernel
• Rings 1 and 2: customized levels of access, generally used by VMs
• Ring 3 (user mode) restricted access to resources

Virtual Memory
Each process has its own "virtual" memory space and resources. Its memory is "virtual" because the process thinks it has a large range of contiguous addresses; but in reality this is implemented by dividing RAM into chunks called pages (4 KB on x86 systems) and having its active pages scattered around RAM and inactive pages stored on disk. The CPU has a transparent mechanism for translating virtual addresses to physical addresses through a page table which the OS sets up. Virtual memory is useful because:
• A process cannot access the memory of other processes
• Each page can have different protection settings (read-only, read-write, kernel-mode-only, etc.)
• Inactive pages can be paged out to disk and retrieved when needed. This is also done when the system is low on RAM.

User Mode
In this mode, programs cannot modify pages directly and so have no way of affecting other processes except through their API. Programs in thismode also cannot interfere with interrupts and context switching.

Kernel Mode
When Windows is first loaded, the Windows kernel is started. It runs in kernel mode and sets up paging, virtual memory, interrupt handlers. Except System which runs in kernel mode, every other process runs in user mode. The kernel then creates some system processes in user mode, but switches back to kernel mode when it is interrupted by interrupts (events such as timers, keyboard, hard disk I/O). Whenever an interrupt occurs, the CPU stops executing the currently running program, switches to kernel mode, and executes the interrupt handler. The handler saves the state of the CPU, performs some processing relevant to that event, and restores the state of the CPU (possibly switching back to user mode) so the CPU can resume execution of the program.

Interrupts
When a program calls a Windows API function, that itself calls a different API: the Native API. Then it either triggers an interrupt or executes instructions such as sysenter and sysexit (x86). Both cause the CPU to switch to ring 0 (kernel mode) and begin executing the desired API function which is the interrupt handler set up by the OS. When the API function has finished processing, it switches back to user mode and resumes execution of the program. This is because API functions like ReadProcessMemory cannot work in user mode; the program can't access other programs' memory. In kernel mode, however, the API function can read any memory region without restriction.

Context Switching
Programs may let the OS to switch to another program because they are waiting for something (human input, hard disk). These programs are known as unrunnable programs, and since they make calls to the kernel to wait for something, the kernel knows to perform context switching to allow another program to run. This is done by:
1. saving the state of the current program (including registers)
2. deciding which program to run next
3. restoring the state of that program

Preemption
It is setting a timed interrupt that will invoke context switching so that if a process (or thread) runs for more than a certain period of time (a process time slice or thread quantum), the OS will switch the context to another program. The time slice that is used may be different for each process.

📚 Summary of: Windows Programming » User Mode versus Kernel Mode

#Windows #Windows_Programming
OpenCL (Open Computing Language)
- is a parallelism framework for writing programs that execute across heterogeneous platforms consisting of one host CPU and any number of compute devices. These devices include GPUs, CPUs with SIMD instructions, FPGAs, Movidius Myriad 2, Adapteva epiphany and DSPs.

A compute device is broken down to several compute units, which themselves are broken down to multiple PEs (processing elements). A single function execution can run on any number of PEs in parallel. How a compute device is subdivided into compute units and PEs is up to the vendor.

It defines an API for programs running on the host to launch functions on compute devices and manage device memory. This API is defined for C and C++, as well as third-parties such as #Python, #Java, #Perl, #DotNET, etc. A more recent, higher-level model is SYCL; which is purely based on C++11. Programs in OpenCL are compiled at run-time therefore its applications are portable between various host devices.

A four-level memory hierarchy is defined for the compute device:
• global: shared by all PEs, high access latency __global
• read-only: smaller, low latency, writable only by the host __constant
• local: shared by a group of PEs __local
• per-element private memory: device registers; __private

Not every device needs to implement each level of this hierarchy in hardware. Consistency between the various levels in the hierarchy is relaxed, and only enforced by explicit synchronization constructs, notably barriers. The host provides handles on device memory buffers and functions to transfer data back and forth.

OpenCL C
- is the programming language used to write compute kernels. Though based on C99, it is adapted to fit the device model.

A memory buffer resides in a specific level and its pointer is annotated with a region qualifier: __global, __local, __constant, and __private

Comparison with #C:
• There is no main; functions are marked __kernel to signal that they are entry points, and are to be called from programs running on the host
• There are no function pointers, bit fields or variable-length arrays
• Recursion is forbidden
stdlib is replaced by a custom set of standard functions, geared toward math programming
• Scalar types such as float and double behave similarly to those of C

Features:
• Vector types available in fixed-lengths of 2, 3, 4, 8 and 16; and for various base types e.g. float4 (4-vector of single-precision floats)
• Operations for vector types
• Synchronization facilities
• Functions to work with work-items and work-groups
• More specialized types incl. 2D and 3D image types

#OpenCL #OpenCL_C
SISAL
"Streams and Iteration in a Single Assignment Language" is a general-purpose single assignment functional programming language with strict semantics, implicit parallelism, and efficient array handling. It was derived from VAL (Value-oriented Algorithmic Language by Jack Dennis), and adds recursion and finite streams.

By: James McGraw
First appeared: 1983
First compiled implementation: 1986
Paradigms: #functional, #dataflow
Syntax: #Pascal -like
Performance: superior to #C and rivals #Fortran

SISAL is more than just a dataflow and fine-grain language; it is a set of tools that convert a textual human readable dataflow language into a graph format (named IF1 - Intermediary Form 1). Part of the SISAL project also involved converting this graph format into runable C code.

In 2010 SISAL saw a brief resurgence when a group of undergraduates at Worcester Polytechnic Institute investigated implementing a fine-grain parallelism backend for the SISAL language.

In 2018 SISAL got modernized with ident-based syntax, first-class functions, lambdas, closures and lazy semantics within project SISAL-IS.

#SISAL
#programming_paradigms
Stream processing
- is a programming paradigm that simplifies parallelism by restricting the parallel computation that can be performed: programs may use multiple computational units, such as the floating point unit on a GPU or FPGA, without explicitly managing allocation, synchronization, or communication among those units. Given a sequence or "stream" of data, a series of kernel functions is applied to each element in that stream.

Kernel functions are usually pipelined, and optimal local on-chip memory reuse is attempted, in order to minimize the loss in bandwidth, accredited to external memory interaction. Uniform streaming, where one kernel function is applied to all elements in the stream, is typical. Since the kernel and stream abstractions expose data dependencies, compiler tools can fully automate and optimize on-chip management tasks. Stream processing hardware can use scoreboarding, for example, to initiate a direct memory access (DMA) when dependencies become known. The elimination of manual DMA management reduces software complexity, and an associated elimination for hardware cached I/O, reduces the data area expanse that has to be involved with service by specialized computational units such as ALUs.

Stream processing was explored within dataflow programming, during the 80s. An example is the language #SISAL.

Compute kernel a.k.a. Kernel function
- is a function compiled for high throughput accelerators, separate from but used by programs running on CPU. They roughly correspond to inner loops when implementing algorithms in traditional languages (though non-sequential), or to code passed to internal iterators. They may be specified by a separate programming language such as #OpenCL_C, or embedded directly in application code written in a high level language, as in the case of C++AMP.

#Stream_processing
Though impossible for them to yield false positives, natural memories do yield false negatives.
There is no importing involved. You just use whichever class you want and you're good to go. Each class has a universally unique name. No more importing giant libraries with tons of unused definitions.
Productivity plummets under temporal pressure.
“The critical design tool for software development is a mind well educated in design principles. It is not UML or any other technology.”
— Craig Larman
“There are 2 types of software engineer: those who understand computer science well enough to do challenging, innovative work, and those who just get by because they’re familiar with a few high level tools.

Both call themselves software engineers, and both tend to earn similar salaries in their early careers. But Type 1 engineers grow in to more fulfilling and well-remunerated work over time, whether that’s valuable commercial work or breakthrough open-source projects, technical leadership or high-quality individual contributions.

Type 1 engineers find ways to learn computer science in depth, whether through conventional means or by relentlessly learning throughout their careers. Type 2 engineers typically stay at the surface, learning specific tools and technologies rather than their underlying foundations, only picking up new skills when the winds of technical fashion change.

Currently, the number of people entering the industry is rapidly increasing, while the number of CS grads is essentially static. This oversupply of Type 2 engineers is starting to reduce their employment opportunities and keep them out of the industry’s more fulfilling work. Whether you’re striving to become a Type 1 engineer or simply looking for more job security, learning computer science is the only reliable path.”

— preface of teachyourselfcs.com
“I have only one method that I recommend extensively;

it’s called think before you write.”
— Richard Hamming
“All progress is the work of individuals.”
— Ayn Rand
IDE
- is any app that increases productivity of development by putting the required programs together so that interacting with them is done through a single interface, eliminating the need for compatibility checks and getting them to work together. It usually consists of at least a specialized text editor, a compiler and a debugger.

Features expected from the text editor:
• Syntax highlighting
• Flexible UI
• Powerful search and navigation tools
• Extensible
• Customizable
#C #Windows

“For those who care about such things: Many have asked whether Windows is written in C or C++. The answer is that – despite NT’s Object-Based design – like most OS’, Windows is almost entirely written in ‘C’. Why? C++ introduces a cost in terms of memory footprint, and code execution overhead. Even today, the hidden costs of code written in C++ can be surprising, but back in the late 1990’s, when memory cost ~$60/MB (yes … $60 per MEGABYTE!), the hidden memory cost of vtables etc. was significant. In addition, the cost of virtual-method call indirection and object-dereferencing could result in very significant performance & scale penalties for C++ code at that time. While one still needs to be careful, the performance overhead of modern C++ on modern computers is much less of a concern, and is often an acceptable trade-off considering its security, readability, and maintainability benefits … which is why we’re steadily upgrading the Console’s code to modern C++.”

Inside the Windows Console
#Windows
The Windows API
- is Microsoft's core set of APIs available in the Windows OS, designed for interactions between apps and the OS.

Though its exposed functions and data structures are described in #C, any compiler or assembler able to handle the low-level data structures and the prescribed calling conventions for calls and callbacks may use it.

The functions provided by the Windows API can be grouped into eight categories:


1. Services
Base Services:
• file systems
• devices
• processes and threads
• error handling
kernel32.dll, KernelBase.dll

Advanced Services:
• the Windows registry
• shutdown/restart the system
• start/stop/create a Windows service
• manage user accounts
advapi32.dll, advapires32.dll


2. Graphics Device Interface
• to output graphics to monitors, printers, etc.
— user-mode: gdi32.dll
— kernel-mode: win32k.sys (communicates directly with the graphics driver)


3. GUI
• to create and manage screen windows
• receive mouse and keyboard input
user32.dll

Common Dialog Box Library:
• to open and save files
• choose color
• choose font
• ...
comdlg32.dll

Common Control Library:
• buttons
• scrollbars
• status bars
• progress bars
• toolbars
• tabs
• ...
comctl32.dll


4. Windows Shell
• access and manipulate functions provided by the shell — shell32.dll
• The Shell Lightweight Utility Functions — shlwapi.dll


5. Network Services
• NetBIOS
• Winsock
• NetDDE
• remote procedure call (RPC)
• ...
netapi32.dll


6. Web
Internet Explorer also exposes an API. IE has been included with the OS since Windows 95 OSR2 and has provided web-related services to apps since Windows 98.
• An embeddable web browser control
• ...
shdocvw.dll, mshtml.dll


7. Multimedia
MCI:
• play sound files
• send/receive MIDI messages
• access joysticks
winmm.dll

Media encoding and playback:
• DirectShow: builds and runs generic multimedia pipelines, used to render in-game videos and build media players, Windows Media Player was based on it, no longer recommended for game development
• Media Foundation: a newer digital media API intended to replace DirectShow


8. DirectX
• Direct2D: hardware-accelerated 2D vector graphics
• Direct3D: hardware-accelerated 3D graphics
• DirectSound: low-level hardware-accelerated sound card access
• DirectInput: communication with input devices such as joysticks and gamepads
• DirectPlay: a multiplayer gaming infrastructure, deprecated
• DirectDraw: for 2D graphics, deprecated and replaced with Direct2D
• WinG: 16-bit 2D graphics, deprecated
#Advice

1. Maintain good posture
2. Use creative names
3. Don't be afraid to write a function
4. Work on your code a little bit at a time
5. Break apart larger projects into several modules
6. Know what a pointer is
7. Use white space before condensing
8. Know when if-else becomes switch-case
9. When you get stuck, read your code out loud
10. Comment why it does that; not what it does

— Summary of Chapter 27: Ten Reminders and Suggestions from Beginning Programming with C For Dummies by Dan Gookin
1. Maintain good posture
For many programmers, coding becomes an obsession: to sit and write code for many hours straight. That's pretty hard on the body. So every few minutes, take a break or even schedule one. Seriously: The next time you compile, stand up! Look outside! Walk around a bit! While you're working, try as hard as you can to keep your shoulders back and your wrists elevated. Don't crook your neck when you look at the monitor. Don't hunch over the keyboard. Look out a window to change your focus.
2. Use creative names
The best code I've seen reads like a human language. It's tough to make the entire source code read that way, but for small snippets, having appropriate variable and function names is a boon to writing clear code.