




"""simple docstring"""


from multiprocessing import Lock, Pipe, Process

# lock used to ensure that two processes do not access a pipe at the same time
A_						=				Lock()


def 					UpperCAmelCase__       (snake_case__							:					int		,   snake_case__							:					Optional[int]		,   snake_case__							:					Dict		,   snake_case__							:					List[Any]		,   snake_case__							:					List[Any]		,   snake_case__							:					int		,   snake_case__							:					Tuple    ):
  """simple docstring"""

  global process_lock

  # we perform n swaps since after n swaps we know we are sorted
  # we *could* stop early if we are sorted already, but it takes as long to
  # find out we are sorted as it does to sort the list with this algorithm
  for i in range(0		,   10    ):
    if (i + position) % 2 == 0 and r_send is not None:
      # send your value to your right neighbor
      process_lock.acquire()
      r_send[1].send(__SCREAMING_SNAKE_CASE    )
      process_lock.release()

      # receive your right neighbor's value
      process_lock.acquire()
      _snake_case							:		str								=  rr_cv[0].recv()
      process_lock.release()

      # take the lower value since you are on the left
      _snake_case							:		Optional[Any]								=  min(__SCREAMING_SNAKE_CASE		,   __SCREAMING_SNAKE_CASE    )
    elif (i + position) % 2 != 0 and l_send is not None:
      # send your value to your left neighbor
      process_lock.acquire()
      l_send[1].send(__SCREAMING_SNAKE_CASE    )
      process_lock.release()

      # receive your left neighbor's value
      process_lock.acquire()
      _snake_case							:		Dict								=  lr_cv[0].recv()
      process_lock.release()

      # take the higher value since you are on the right
      _snake_case							:		str								=  max(__SCREAMING_SNAKE_CASE		,   __SCREAMING_SNAKE_CASE    )
    # after all swaps are performed, send the values back to main
  result_pipe[1].send(__SCREAMING_SNAKE_CASE    )


def 					UpperCAmelCase__       (snake_case__							:					Any    ):
  """simple docstring"""

  _snake_case							:		Optional[Any]								=  []
  _snake_case							:		int								=  []
  # initialize the list of pipes where the values will be retrieved
  for _ in arr:
    result_pipe.append(Pipe()    )
  # creates the processes
  # the first and last process only have one neighbor so they are made outside
  # of the loop
  _snake_case							:		List[str]								=  Pipe()
  _snake_case							:		str								=  Pipe()
  process_array_.append(
      Process(
          target=__SCREAMING_SNAKE_CASE		,   args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0])		,   )    )
  _snake_case							:		Tuple								=  temp_rs
  _snake_case							:		Optional[Any]								=  temp_rr

  for i in range(1		,   len(__SCREAMING_SNAKE_CASE    ) - 1    ):
    _snake_case							:		int								=  Pipe()
    _snake_case							:		Tuple								=  Pipe()
    process_array_.append(
        Process(
            target=__SCREAMING_SNAKE_CASE		,   args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i])		,   )    )
    _snake_case							:		Dict								=  temp_rs
    _snake_case							:		int								=  temp_rr

  process_array_.append(
      Process(
          target=__SCREAMING_SNAKE_CASE		,   args=(
              len(__SCREAMING_SNAKE_CASE    ) - 1,
              arr[len(__SCREAMING_SNAKE_CASE    ) - 1],
              temp_ls,
              None,
              temp_lr,
              None,
              result_pipe[len(__SCREAMING_SNAKE_CASE    ) - 1],
          )		,   )    )

  # start the processes
  for p in process_array_:
    p.start()

  # wait for the processes to end and write their values to the list
  for p in range(0		,   len(__SCREAMING_SNAKE_CASE    )    ):
    _snake_case							:		int								=  result_pipe[p][0].recv()
    process_array_[p].join()
  return arr


def 					UpperCAmelCase__       ():
  """simple docstring"""

  _snake_case							:		List[Any]								=  list(range(10		,   0		,   -1    )    )
  print("""Initial List"""    )
  print(*__SCREAMING_SNAKE_CASE    )
  _snake_case							:		Any								=  odd_even_transposition(__SCREAMING_SNAKE_CASE    )
  print("""Sorted List\n"""    )
  print(*__SCREAMING_SNAKE_CASE    )


if __name__ == "__main__":
     main()




from manim import *







class        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    Rectangle(height=0.5		,    width=0.5       )
		lowercase       =    Rectangle(height=0.25		,    width=0.25       )
		lowercase       =    Rectangle(height=0.46		,    width=0.46       ).set_stroke(width=0       )

		lowercase       =    [mem.copy() for i in range(6       )]
		lowercase       =    [mem.copy() for i in range(6       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    VGroup(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    Text('CPU'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		cpu.move_to([-2.5, -0.5, 0]       )
		self.add(snake_case       )

		lowercase       =    [mem.copy() for i in range(4       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    Text('GPU'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		gpu.move_to([-1, -1, 0]       )
		self.add(snake_case       )

		lowercase       =    [mem.copy() for i in range(6       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )

		lowercase       =    Text('Model'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		model.move_to([3, -1.0, 0]       )
		self.add(snake_case       )

		lowercase       =    []
		lowercase       =    []
		lowercase       =    []

		for i, rect in enumerate(snake_case       ):
			rect.set_stroke(snake_case       )

			lowercase       =    Rectangle(height=0.46 / 4		,    width=0.46 / 3       ).set_stroke(width=0.0       ).set_fill(snake_case		,    opacity=0.7       )

			if i == 0:
				cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT       )		,    buff=0.02		,    direction=snake_case       )
				cpu_target.set_x(cpu_target.get_x() + 0.1       )
			elif i == 3:
				cpu_target.next_to(model_cpu_arr[0]		,    direction=snake_case		,    buff=0.0       )
			else:
				cpu_target.next_to(model_cpu_arr[i - 1]		,    direction=snake_case		,    buff=0.0       )
			self.add(snake_case       )
			model_cpu_arr.append(snake_case       )

		self.add(*snake_case		,    *snake_case		,    *snake_case       )

		lowercase       =    [mem.copy() for i in range(6       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )

		lowercase       =    Text('Loaded Checkpoint'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		checkpoint.move_to([3, 0.5, 0]       )

		self.add(snake_case       )

		lowercase       =    []
		lowercase       =    []

		for i, rect in enumerate(snake_case       ):
			lowercase       =    fill.copy().set_fill(snake_case		,    opacity=0.7       )
			target.move_to(snake_case       )
			ckpt_arr.append(snake_case       )

			lowercase       =    target.copy()
			if i < 5:
				cpu_target.move_to(cpu_left_col_base[i + 1]       )
			else:
				cpu_target.move_to(cpu_right_col_base[i - 5]       )
			ckpt_cpu_arr.append(snake_case       )
		self.add(*snake_case		,    *snake_case       )

		lowercase       =    Square(side_length=2.2       )
		key.move_to([-5, 2, 0]       )

		lowercase       =    MarkupText(
		    F'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'''		,    font_size=18		,    )

		key_text.move_to([-5, 2.4, 0]       )

		self.add(snake_case		,    snake_case       )

		lowercase       =    MarkupText(
		    F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'''		,    font_size=18		,    )

		blue_text.next_to(snake_case		,    DOWN * 2.4		,    aligned_edge=key_text.get_left()       )
		self.add(snake_case       )

		lowercase       =    MarkupText(
		    F'''Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.'''		,    font_size=24		,    )
		step_a.move_to([2, 2, 0]       )

		lowercase       =    [meta_mem.copy() for i in range(6       )]
		lowercase       =    [meta_mem.copy() for i in range(6       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    VGroup(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    Text('Disk'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		disk.move_to([-4.0, -1.25, 0]       )
		self.play(Write(snake_case		,    run_time=3       )		,    Write(snake_case		,    run_time=1       )		,    Create(snake_case		,    run_time=1       )       )

		lowercase       =    []
		for i, rect in enumerate(snake_case       ):
			lowercase       =    rect.copy()
			target.generate_target()
			target.target.move_to(disk_left_col_base[i]       ).scale(0.5       )
			animations.append(MoveToTarget(snake_case		,    run_time=1.5       )       )
		self.play(*snake_case       )

		self.play(FadeOut(snake_case       )       )

		lowercase       =    MarkupText(F'''Then, the checkpoint is removed from memory\nthrough garbage collection.'''		,    font_size=24       )
		step_a.move_to([2, 2, 0]       )

		self.play(Write(snake_case		,    run_time=3       )       )

		self.play(
		    FadeOut(snake_case		,    snake_case		,    *snake_case		,    *snake_case       )		,    )

		self.wait()

