Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,6 @@
|
|||
package Synchronous_Concurrent is
|
||||
task Printer is
|
||||
entry Put(Item : in String);
|
||||
entry Get_Count(Count : out Natural);
|
||||
end Printer;
|
||||
end Synchronous_Concurrent;
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
|
||||
|
||||
package body Synchronous_Concurrent is
|
||||
|
||||
task body Printer is
|
||||
Num_Iter : Natural := 0;
|
||||
Line : Unbounded_String;
|
||||
begin
|
||||
loop
|
||||
select
|
||||
accept Put(Item : in String) do
|
||||
Line := To_Unbounded_String(Item);
|
||||
end Put;
|
||||
Put_Line(To_String(Line));
|
||||
Num_Iter := Num_Iter + 1;
|
||||
or
|
||||
accept Get_Count(Count : out Natural) do
|
||||
Count := Num_Iter;
|
||||
end Get_Count;
|
||||
or terminate;
|
||||
end select;
|
||||
end loop;
|
||||
end Printer;
|
||||
|
||||
end Synchronous_Concurrent;
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
with Synchronous_Concurrent; use Synchronous_Concurrent;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Synchronous_Concurrent_Main is
|
||||
Num_Strings : Natural;
|
||||
The_File : File_Type;
|
||||
Line : String(1..255);
|
||||
Length : Natural;
|
||||
begin
|
||||
Open(File => The_File, Mode => In_File, Name => "input.txt");
|
||||
while not End_Of_File(The_File) loop
|
||||
Get_Line(File => The_File, Item => Line, Last => Length);
|
||||
Printer.Put(Line(1..Length));
|
||||
end loop;
|
||||
Close(The_File);
|
||||
Printer.Get_Count(Num_Strings);
|
||||
New_Line;
|
||||
Put_Line("The task wrote" & Natural'Image(Num_Strings) & " strings.");
|
||||
end Synchronous_Concurrent_Main;
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
sequence lines
|
||||
sequence count
|
||||
lines = {}
|
||||
count = {}
|
||||
|
||||
procedure read(integer fn)
|
||||
object line
|
||||
while 1 do
|
||||
line = gets(fn)
|
||||
if atom(line) then
|
||||
exit
|
||||
else
|
||||
lines = append(lines, line)
|
||||
task_yield()
|
||||
end if
|
||||
end while
|
||||
|
||||
lines = append(lines,0)
|
||||
while length(count) = 0 do
|
||||
task_yield()
|
||||
end while
|
||||
|
||||
printf(1,"Count: %d\n",count[1])
|
||||
end procedure
|
||||
|
||||
procedure write(integer fn)
|
||||
integer n
|
||||
object line
|
||||
n = 0
|
||||
while 1 do
|
||||
while length(lines) = 0 do
|
||||
task_yield()
|
||||
end while
|
||||
|
||||
line = lines[1]
|
||||
lines = lines[2..$]
|
||||
if atom(line) then
|
||||
exit
|
||||
else
|
||||
puts(fn,line)
|
||||
n += 1
|
||||
end if
|
||||
end while
|
||||
count = append(count,n)
|
||||
end procedure
|
||||
|
||||
integer fn
|
||||
atom reader, writer
|
||||
constant stdout = 1
|
||||
fn = open("input.txt","r")
|
||||
reader = task_create(routine_id("read"),{fn})
|
||||
writer = task_create(routine_id("write"),{stdout})
|
||||
task_schedule(writer,1)
|
||||
task_schedule(reader,1)
|
||||
|
||||
while length(task_list()) > 1 do
|
||||
task_yield()
|
||||
end while
|
||||
112
Task/Synchronous-concurrency/Fortran/synchronous-concurrency.f
Normal file
112
Task/Synchronous-concurrency/Fortran/synchronous-concurrency.f
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
program concurrent_line_printer
|
||||
! Uses OpenMP to create 2 threads and manage the communication between them.
|
||||
use omp_lib
|
||||
implicit none
|
||||
|
||||
character(len=256) :: shared_line
|
||||
integer :: shared_count
|
||||
integer :: state ! 0=ready, 1=line_available, 2=reading_done, 3=count_available
|
||||
|
||||
state = 0
|
||||
shared_count = 0
|
||||
|
||||
!$omp parallel sections num_threads(2)
|
||||
|
||||
!$omp section
|
||||
call reader_thread()
|
||||
|
||||
!$omp section
|
||||
call printer_thread()
|
||||
|
||||
!$omp end parallel sections
|
||||
|
||||
contains
|
||||
|
||||
subroutine reader_thread()
|
||||
integer :: io_stat, unit_num, final_count
|
||||
character(len=256) :: line
|
||||
|
||||
open(newunit=unit_num, file='input.txt', status='old', action='read', iostat=io_stat)
|
||||
if (io_stat /= 0) then
|
||||
print *, "Error: Cannot open input.txt"
|
||||
!$omp atomic write
|
||||
state = 2
|
||||
return
|
||||
end if
|
||||
|
||||
! Read and send each line
|
||||
do
|
||||
read(unit_num, '(A)', iostat=io_stat) line
|
||||
if (io_stat /= 0) exit
|
||||
|
||||
! Wait for printer to be ready
|
||||
do while (state /= 0)
|
||||
!$omp flush(state)
|
||||
end do
|
||||
|
||||
! Send line to printer
|
||||
shared_line = line
|
||||
!$omp flush(shared_line)
|
||||
!$omp atomic write
|
||||
state = 1
|
||||
end do
|
||||
|
||||
close(unit_num)
|
||||
|
||||
! Wait for last line to be processed
|
||||
do while (state == 1)
|
||||
!$omp flush(state)
|
||||
end do
|
||||
|
||||
! Signal reading is done
|
||||
!$omp atomic write
|
||||
state = 2
|
||||
|
||||
! Wait for count to be available
|
||||
do while (state /= 3)
|
||||
!$omp flush(state)
|
||||
end do
|
||||
|
||||
!$omp flush(shared_count)
|
||||
final_count = shared_count
|
||||
|
||||
print *, "Number of lines printed by printer thread:", final_count
|
||||
|
||||
end subroutine reader_thread
|
||||
|
||||
subroutine printer_thread()
|
||||
character(len=256) :: line
|
||||
integer :: count, current_state
|
||||
|
||||
count = 0
|
||||
|
||||
do
|
||||
! Wait for line or done signal
|
||||
do
|
||||
!$omp flush(state)
|
||||
current_state = state
|
||||
if (current_state /= 0) exit
|
||||
end do
|
||||
|
||||
if (current_state == 2) exit ! Reading done
|
||||
|
||||
! Get and print line
|
||||
!$omp flush(shared_line)
|
||||
line = shared_line
|
||||
count = count + 1
|
||||
print *, trim(line)
|
||||
|
||||
! Signal line processed
|
||||
!$omp atomic write
|
||||
state = 0
|
||||
end do
|
||||
|
||||
! Send count back to reader
|
||||
shared_count = count
|
||||
!$omp flush(shared_count)
|
||||
!$omp atomic write
|
||||
state = 3
|
||||
|
||||
end subroutine printer_thread
|
||||
|
||||
end program concurrent_line_printer
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
$define EOT = "\x04"
|
||||
|
||||
local function read_lines(filename, numlines)
|
||||
local text = io.contents(filename) or ""
|
||||
local nl = os.platform == "windows" ? "\r\n" : "\n"
|
||||
local lines = text:rstrip():split(nl)
|
||||
for lines as line do
|
||||
coroutine.yield(line)
|
||||
end
|
||||
coroutine.yield(EOT)
|
||||
_, numlines = coroutine.yield()
|
||||
print($"Number of lines read = {numlines}")
|
||||
end
|
||||
|
||||
local co = coroutine.create(read_lines)
|
||||
local filename = "input.txt"
|
||||
local numlines = 0
|
||||
while true do
|
||||
local _, line = coroutine.resume(co, filename, numlines)
|
||||
if !line then break end
|
||||
if line == EOT then
|
||||
coroutine.resume(co, filename, numlines)
|
||||
else
|
||||
numlines += 1
|
||||
print(line)
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue